

nginx只允许域名访问,禁止ip访问
source link: https://www.cnblogs.com/tuyile006/p/15800440.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

nginx只允许域名访问,禁止ip访问
背景:
为什么要禁止ip访问页面呢?
这样做是为了避免其他人把未备案的域名解析到自己的服务器IP,而导致服务器被断网,我们可以通过禁止使用ip访问的方法,防止此类事情的发生。
解决方法:
这里介绍修改配置文件nginx.conf两种方法:
1)在server段里插入如下正则:
listen 80;
server_name www.yuyangblog.net;
if ($host != 'www.yuyangblog.net'){
return 403;
}
2)添加一个server
新加的server(注意是新增,并不是在原有的server基础上修改)
server {
listen 80 default;
server_name _;
return 403;
}
原来server里面插入:
listen 80;
server_name www.yuyangblog.net;
设置成功后,就只能用域名访问网站,不能用ip访问了。
实例配置:
[root@weifeng conf.d]# cat weifeng.conf
server {
listen 80 default;
server_name _;
return 403;
}
server { server_name 80 default; server_name _; rewrite ^(.*) http://www.demo.com; } server { server_name localhost www.demo.com; root /usr/share/nginx/www; index index.html index.htm; location / { index index.php; try_files $uri $uri/ /index.html; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } }
Recommend
-
27
-
40
-
26
-
30
Nginx配置多端口多域名访问 Nov 18th, 2019 | Comments
-
5
11 April 2020 / 开发笔记 Nginx 配置允许跨域访问 CORS 原文地址: https://en...
-
6
Nginx 禁止未绑定域名或 IP 访问 80 和 443 端口实践小结nginx 在决定请求由哪个 server 块执行时,主要关注的是 server 块中的 listen 和 server_name 两个字段,如果根据 listen 指令无法得到最佳匹配,将会开始解析 server_name 指令。nginx 会检查请求中的...
-
11
怕学生会看HTML源码里的答案,Chrome允许网管禁止查看特定网站源码 Posted 2021-11-10 | stdin 看到一个奇葩issue With "view-source"...
-
7
V2EX › NGINX NGINX 求助 禁止访问 https://ip 后,前端机器 proxy 502 ab · 12 小时 42 分钟前 ·...
-
3
Nginx代理配置只允许指定IP访问 精选 原创 码农小宋 2023-01-08 19:04:08...
-
23
V2EX › 宽带症候群 routeros 里该怎么禁止某些域名访问呢
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK