一、不带www的域名加301跳转
如果不带www的域名要加301跳转,那也是和绑定域名一样,先绑定不带www的域名,只是不用写网站目录,而是进行301跳转,如
server{ listen 80; server_name b.net; rewrite ^/(.*) http://www.b.net/$1 permanent; }
二、添加404网页
添加404网页,都可直接在里面添加,如:
server { listen 80; server_name www.c.net; #绑定域名 index index.htm index.html index.php; #默认文件 root /home/www/c.net; #网站根目录 #include location.conf; #调用其他规则,也可去除 error_page 404 /404.html; }
三、限制ip和限制ip段
server{ listen 80; server_name xx.qq.com; root /home/data/xx; location / { auth_basic "closed site"; auth_basic_user_file /etc/nginx/conf/auth/xx; allow 10.19.0.0/16; # 限制ip段 #allow 192.168.0.54; # 限制某个ip allow 127.0.0.1; deny all; try_files $uri $uri/ /index.php?$query_string; index index.php; location ~ \.php$ { fastcgi_pass php-fpm-8000; include conf/public_config/fcgi.conf; } include conf/public_config/secure_base.conf; include conf/public_config/static.conf; } access_log /home/data/logs/xx.access;}
四、公网调用强制跳转到https,内网调用可以正常调用http
因为一个业务系统,既要提供公网访问服务又要提供内部系统调用。所以才有这种需求(有这种需求也是我们安全团队要求强制https的),而我们的内网段都是172.x段的
## 公网http调用,强制跳转到https if ($remote_addr !~ ^(172)\.(19|18)\.(.*)\.(.*)$) { rewrite ^(.*)$ https://$host$1 permanent; }
五:返回自定义内容
## 公网http无法调用 if ($remote_addr !~ ^(172)\.(19|18)\.(.*)\.(.*)$) { return 200 "此域名不支持公网调用,相关问题咨询请联系tom"; }
六:我们再玩得更高级一点
因为如果我们的网站内容很多,但是如果修改了配置需要改每个配置文件,所以我们想直接通过引入的方式。
vim conf/public_config/only_ext_https.conf
## 公网http调用,强制跳转到httpsif ($remote_addr !~ ^(172)\.(19|18)\.(.*)\.(.*)$) { rewrite ^(.*)$ https://$host$1 permanent;}
我们来看Nginx的配置:
upstream api { server localhost:8087 max_fails=3 fail_timeout=3s weight=1;} server{ listen 80; server_name mp-dev.qq.cn admin-mp-dev.qq.cn; root /data/data/webroot/mp.qq.cn/dist; ## 公网http调用,强制跳转到https include conf/public_config/only_ext_https.conf; location /download/xx-mall { alias /data/data/images/upload/xx-mall; } location / { index index.html index.php; include conf/public_config/secure_base.conf; include conf/public_config/static.conf; include conf/public_config/access_log.conf; } access_log /data/data/logs/mp-dev.qq.cn/mp-dev.qq.cn.access.log access; access_log /data/data/logs/mp-dev.qq.cn/mp-dev.qq.cn.access.logstash_json logstash_json;}