1. Nginx 通配符匹配
按照匹配规则的优先级,由高到低:
= 表示精确匹配
^~ 表示uri以某个常规字符串开头,大多情况下用来匹配url路径,nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格,即所见即所得)。
~ 正则匹配(区分大小写)
~* 正则匹配(不区分大小写)
!~ 和 !~* 分别为区分大小写不匹配及不区分大小写不匹配 的正则
/ 任何请求都会匹配
2. Nginx 通配符优先级
首先匹配 =
,其次匹配 ^~
, 其次是按文件中顺序的正则匹配,最后是交给 /
通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。
location = / {
#规则A
}
location = /login {
#规则B
}
location ^~ /static/ {
#规则C
}
location ~ \.(gif|jpg|png|js|css)$ {
#规则D
}
location ~* \.png$ {
#规则E
}
location !~ \.xhtml$ {
#规则F
}
location !~* \.xhtml$ {
#规则G
}
location / {
#规则H
}
访问根目录/, 比如http://localhost/ 将匹配规则A
访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H
访问 http://localhost/static/a.html 将匹配规则C
访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则D,规则E不起作用,而 http://localhost/static/c.png 则优先匹配到规则C
访问 http://localhost/a.PNG 则匹配规则E,而不会匹配规则D,因为规则E不区分大小写。
访问 http://localhost/a.xhtml 不会匹配规则F和规则G,http://localhost/a.XHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。
访问 http://localhost/category/id/1111 则最终匹配到规则H,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。
3.简单通用配置
静态动态分离,除了静态资源以外的请求都交给Tomcat处理.
location / {
proxy_pass http://[tomcatip]:[80]
}
location ^~ /static/ {
root /webroot/static/;
}
location ~* \.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
root /webroot/res/;
}
4.生产环境配置举例
生产环境的配置文件主要分布在三个文件夹中:conf
、conf.d
、default.d
。起到的作用可以自己定义的。
4.1 nginx.conf
#user nobody;
worker_processes auto;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
worker_rlimit_nofile 10240;
events {
use epoll;
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
large_client_header_buffers 4 64k;
client_max_body_size 20m;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 3;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
include /usr/local/nginx/conf.d/*.conf;
server {
listen 80;
server_name 172.16.19.114;
include /usr/local/nginx/default.d/*.conf;
#charset koi8-r;
#access_log logs/host.access.log main;
# location / {
# root 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 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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
4.2 upstream.conf
#设置tomcat负载均衡主机
upstream tomcatHost {
least_conn;
server 172.16.19.114:8081 weight=100 max_fails=12 fail_timeout=60s;
keepalive 768;
}
4.3 location.conf
#设置host主机
#以frk开头的服务
location ^~frk {
proxy_pass http://tomcatHost;
}
#以_web结尾的服务
location ~*_web {
root /usr/local/nginx/webapps/;
index index.html index.htm;
}
#以_service结尾的服务
location ~*_service {
proxy_pass http://tomcatHost;
}
#以public为路径的服务
location /public/ {
proxy_pass http://tomcatHost;
}
#默认服务
location / {
alias /usr/local/nginx/webapps/portal_web;
index index.html index.htm;
}