nginx location配置讲解

location语法:表示uri方式定位

基础语法有三种:

  location = pattern {}  精准匹配

  location pattern {}  一般匹配

  location ~ pattern {}  正则匹配

语法规则: location [=|~|~*|^~] /uri/ { … }
= 开头表示精确匹配
^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。
~ 开头表示区分大小写的正则匹配
~*  开头表示不区分大小写的正则匹配
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则
/ 通用匹配,任何请求都会匹配到。
多个location配置的情况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,不必拘泥,仅供参考):
首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。
 

nginx.conf配置说明:

  1 #user  nobody;
  2 
  3 #开启进程数 <=CPU数 
  4 worker_processes  1;
  5 
  6 #错误日志保存位置
  7 #error_log  logs/error.log;
  8 #error_log  logs/error.log  notice;
  9 #error_log  logs/error.log  info;
 10 
 11 #进程号保存文件
 12 #pid        logs/nginx.pid;
 13 
 14 #每个进程最大连接数(最大连接=连接数x进程数)每个worker允许同时产生多少个链接,默认1024
 15 events {
 16     worker_connections  1024;
 17 }
 18 
 19 
 20 http {
 21     #文件扩展名与文件类型映射表
 22     include       mime.types;
 23     #默认文件类型
 24     default_type  application/octet-stream;
 25 
 26     #日志文件输出格式 这个位置相于全局设置
 27     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 28                       '$status $body_bytes_sent "$http_referer" '
 29                       '"$http_user_agent" "$http_x_forwarded_for"';
 30 
 31     #请求日志保存位置
 32     #access_log  logs/access.log  main;
 33     
 34     #打开发送文件
 35     sendfile        on;
 36     #tcp_nopush     on;
 37 
 38     #keepalive_timeout  0;
 39     #连接超时时间
 40     keepalive_timeout  65;
 41 
 42     #打开gzip压缩
 43     #gzip  on;
 44     
 45     #设定请求缓冲
 46     #client_header_buffer_size 1k;
 47     #large_client_header_buffers 4 4k;
 48     
 49     #设定负载均衡的服务器列表
 50     #upstream myproject {
 51         #weigth参数表示权值,权值越高被分配到的几率越大
 52         #max_fails 当有#max_fails个请求失败,就表示后端的服务器不可用,默认为1,将其设置为0可以关闭检查
 53         #fail_timeout 在以后的#fail_timeout时间内nginx不会再把请求发往已检查出标记为不可用的服务器
 54     #}
 55     
 56     #webapp
 57     #upstream myapp {   
 58       # server 192.168.1.171:8080 weight=1 max_fails=2 fail_timeout=30s;   
 59     # server 192.168.1.172:8080 weight=1 max_fails=2 fail_timeout=30s;   
 60     #} 
 61 
 62     #配置虚拟主机,基于域名、ip和端口
 63     server {
 64         #监听端口
 65         listen       80;
 66         #监听域名
 67         server_name  localhost;
 68 
 69         #charset koi8-r;
 70         
 71         #nginx访问日志放在logs/host.access.log下,并且使用main格式(还可以自定义格式)
 72         #access_log  logs/host.access.log  main;
 73 
 74         #返回的相应文件地址
 75         location / {
 76             #设置客户端真实ip地址
 77             #proxy_set_header X-real-ip $remote_addr;        
 78             #负载均衡反向代理
 79             #proxy_pass http://myapp;
 80             
 81             #返回根路径地址(相对路径:相对于/usr/local/nginx/)
 82             root   html;
 83             #默认访问文件
 84             index  index.html index.htm;
 85         }
 86 
 87         #配置反向代理tomcat服务器:拦截.jsp结尾的请求转向到tomcat
 88         #location ~ .jsp$ {
 89         #    proxy_pass http://192.168.1.171:8080;
 90         #}        
 91         
 92         #error_page  404              /404.html;
 93         # redirect server error pages to the static page /50x.html
 94         #
 95         
 96         #错误页面及其返回地址
 97         error_page   500 502 503 504  /50x.html;
 98         location = /50x.html {
 99             root   html;
100         }
101 
102         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
103         #
104         #location ~ .php$ {
105         #    proxy_pass   http://127.0.0.1;
106         #}
107 
108         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
109         #
110         #location ~ .php$ {
111         #    root           html;
112         #    fastcgi_pass   127.0.0.1:9000;
113         #    fastcgi_index  index.php;
114         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
115         #    include        fastcgi_params;
116         #}
117 
118         # deny access to .htaccess files, if Apache's document root
119         # concurs with nginx's one
120         #
121         #location ~ /.ht {
122         #    deny  all;
123         #}
124     }
125     
126     #虚拟主机配置:
127     server {
128         listen 1234;
129         server_name bhz.com;
130         location / {
131         #正则表达式匹配uri方式:在/usr/local/nginx/bhz.com下 建立一个test123.html 然后使用正则匹配
132         #location ~ test {
133             ## 重写语法:if return (条件 = ~ ~*)
134             #if ($remote_addr = 192.168.1.200) {
135             #       return 401;
136             #}        
137             
138             #if ($http_user_agent ~* firefox) {
139             #       rewrite ^.*$ /firefox.html;
140             #       break;
141             #}            
142                         
143             root bhz.com;
144             index index.html;
145         }
146         
147         #location /goods {
148         #        rewrite "goods-(d{1,5}).html" /goods-ctrl.html;
149         #        root bhz.com;
150         #        index index.html;
151         #}
152         
153         #配置访问日志
154         access_log logs/bhz.com.access.log main;
155     }
156     
157 
158 
159     # another virtual host using mix of IP-, name-, and port-based configuration
160     #
161     #server {
162     #    listen       8000;
163     #    listen       somename:8080;
164     #    server_name  somename  alias  another.alias;
165 
166     #    location / {
167     #        root   html;
168     #        index  index.html index.htm;
169     #    }
170     #}
171 
172 
173     # HTTPS server
174     #
175     #server {
176     #    listen       443 ssl;
177     #    server_name  localhost;
178 
179     #    ssl_certificate      cert.pem;
180     #    ssl_certificate_key  cert.key;
181 
182     #    ssl_session_cache    shared:SSL:1m;
183     #    ssl_session_timeout  5m;
184 
185     #    ssl_ciphers  HIGH:!aNULL:!MD5;
186     #    ssl_prefer_server_ciphers  on;
187 
188     #    location / {
189     #        root   html;
190     #        index  index.html index.htm;
191     #    }
192     #}
193 
194 }
原文地址:https://www.cnblogs.com/116970u/p/10891531.html