nginx多server优先级
1 |
在开始处理一个http请求时,nginx会取出header头中的Host变量(域名),与nginx.conf中的每个server_name进行匹配,以此决定到底由哪一个server来处理这个请求,但nginx如果配置多个相同的server_name,会导致server_name出现优先级访问冲突。 |
多server优先级总结
1 2 3 4 5 6 |
1.请求的内容完全匹配server_name 2.选择通配符在前面的server_name *.test.com 3.选择通配符在后面的server_name www.test.* 4.选择正则表达式匹配上的server_name ~^linux\.(.*)\.com$ 5.选择在listen后面加了default_server的server块 6.如果没有配置文件可以匹配,则从上往下访问配置并返回第一个server的配置 |
nginx禁止IP访问网站
禁止IP访问直接返回错误
1 2 3 4 5 6 |
[root@web01 /etc/nginx/conf.d]# vim a_ip.conf server { listen 80 default_server; server_name localhost; return 500; } |
引流的方式跳转页面
1 2 3 4 5 6 7 |
[root@web01 /etc/nginx/conf.d]# vim a_ip.conf server { listen 80 default_server; server_name localhost; rewrite (.*) http://www.baidu.com$1; #return 302 http://www.baidu.com$request_uri; } |
返回指定内容
1 2 3 4 5 6 7 |
[root@web01 /etc/nginx/conf.d]# vim a_ip.conf server { listen 80 default_server; server_name localhost; default_type text/plain; return 200 "请请求其他页面...或使用域名请求!"; } |
nginx的include
1 2 3 4 5 6 7 8 |
一台服务器配置多个网站,如果配置都写在nginx.conf主配置文件中,会导致nginx.conf主配置文件变得非常庞大而且可读性非常的差。那么后期的维护就变得麻烦。 假设现在希望快速的关闭一个站点,该怎么办? 1.如果是写在nginx.conf中,则需要手动注释,比较麻烦 2.如果是include的方式,那么仅需修改配置文件的扩展名,即可完成注释 Include包含的作用是为了简化主配置文件,便于人类可读。 inlcude /etc/nginx/online/*.conf #线上使用的配置 mv /etc/nginx/online/test.conf /etc/nginx/offline/ #保留配置,不启用(下次使用在移动到online中) |
Nginx调整上传文件大小
nginx上传文件大小限制配置语法
1 2 3 |
Syntax: client_max_body_size size; Default: client_max_body_size 1m; Context: http, server, location |
nginx上传文件大小限制配置示例
1 2 3 4 5 6 |
#也可以放入http层,全局生效 server { listen 80; server_name _; client_max_body_size 200m; } |
Nginx优雅显示错误页面
跳转到网上
1 2 3 4 5 6 7 8 9 10 11 12 |
#error_page配置的是http这种的网络地址 [root@web01 conf.d]# cat error.conf server { listen 80; server_name linux.error.com; location / { root /code/error; index index.html; error_page 404 http://www.baidu.com; } } |
跳转本地文件
1 2 3 4 5 6 7 8 9 10 11 |
[root@web01 /code/error]# vim /etc/nginx/conf.d/error.conf server { listen 80; server_name linux.error.com; location / { root /code/error; index index.html; error_page 404 403 /404.jpg; } } |
访问PHP的错误页面跳转
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@web01 /code/error]# vim /etc/nginx/conf.d/error.conf server { listen 80; server_name linux.error.com; root /code/error; index index.php; error_page 404 403 /404.html; location ~* \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; if (!-e $request_filename) { rewrite (.*) http://linux.error.com/404.jpg; } } } |