内容简介:在最开始配置 nginx 的时候,是修改的很简单的配置就可以。多提一句,在
导语
在最开始配置 nginx 的时候,是修改的 default.conf
文件。文件中显示指定了 listen 80 default_server;
,也就是没有匹配到的域名会转到这里来处理。接下来修改为只匹配设置的域名,其他返回 404。
解决方案
很简单的配置就可以。
server {
listen 80 default_server;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
return 404;
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
多提一句,在 nginx.conf
中 http
内添加 server_tokens off;
可以隐藏版本号。
配置好之后,记得 重启 nginx。
重点在于 listen 80 default_server;
和 return 404
,如此配置,没有匹配到的域名,包括直接 ip 访问,都会 404;
以下是我测试配置的流程,各位感兴趣可以看下。
修改流程
- 在服务器供应商中添加一条解析,配置子域名绑定到服务器中
- 首先是到代理 nginx,访问成功,是 nginx 默认的页面。根据上面进行修改后,再次访问会返回 404
- 为了进一步测试,配置 nginx 代理,将刚才绑定的子域名转发到 laradock 的 nginx 中。此时访问会到 laravel 的首页
-
因为之前配置的原因,首先修改
laradock/nginx/sites/laravel.conf.example文件,修改文件名为laravel.conf,主要是server_name和root,内容如下
server {
listen 80;
listen [::]:80;
# For https
# listen 443 ssl;
# listen [::]:443 ssl ipv6only=on;
# ssl_certificate /etc/nginx/ssl/default.crt;
# ssl_certificate_key /etc/nginx/ssl/default.key;
server_name www.you_site.com;
root /var/www/web/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass php-upstream;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fixes timeouts
fastcgi_read_timeout 600;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
location /.well-known/acme-challenge/ {
root /var/www/letsencrypt/;
log_not_found off;
}
error_log /var/log/nginx/laravel_error.log;
access_log /var/log/nginx/laravel_access.log;
}
-
接下来将
default.conf修改为解决方案中的配置,然后 重启 - 此时再访问就是 404
参考资料: Nginx 的 default_server 指令 、 nginx 修改并隐藏版本号 。
以上所述就是小编给大家介绍的《Nginx 禁止未匹配域名访问》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- Golang中实现禁止拷贝
- 使用 ESLint 禁止项目导入特定模块
- TikTok宣布禁止“误导性信息”
- HashiCorp 禁止其企业软件在中国使用
- 禁止站外提交表单(author:killer)
- Chrome 66 禁止声音自动播放,开发怎么应对?
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Cracking the Coding Interview
Gayle Laakmann McDowell / CareerCup / 2015-7-1 / USD 39.95
Cracking the Coding Interview, 6th Edition is here to help you through this process, teaching you what you need to know and enabling you to perform at your very best. I've coached and interviewed hund......一起来看看 《Cracking the Coding Interview》 这本书的介绍吧!