nginx location

nginx location

1.1 location匹配

语法:

location [=|~|~*|^~] /uri/ { … }
=         严格匹配。如果请求匹配这个location,那么将停止搜索并立即处理此请求
~         包含正则表达式并且区分大小写,并且匹配
!~        包含正则表达式并且区分大小写,并且不匹配

~*        包含正则表达式并且不区分大小写,并且匹配
!~*		  包含正则表达式并且不区分大小写,并且不匹配
^~        包含正则表达式并且匹配以什么开头
$		  包含正则表达式并且匹配以什么结尾
\		  包含正则表达式并且转义字符,可以转 . * ?等
*		  包含正则表达式并且代表任意长度的任意字符

2.1 应用实例

2.1.1 简单的实例

    server {
        listen       8080;
        listen       [::]:8080;
        server_name  _;
        location / {	#浏览器页面打开http://192.168.50.121:8080回车访问/usr/share/nginx/html/lili/index.html
	  root /usr/share/nginx/html/lili;
	}
        location /dev {	#浏览器页面打开http://192.168.50.121:8080/dev会访问到/usr/share/nginx/html/about/index.html
	  alias /usr/share/nginx/html/about;
	  index index.html;
	}
	
	#精准匹配,浏览器页面打开http://192.168.50.121:8080/1.jpg会访问到http://192.168.50.121:8080/image/1.jpg资源
	location = /1.jpg {	
	  root /usr/share/nginx/html/image/;
	  index index.html;
	}
	
	#区分大小写,浏览器页面打开http://192.168.50.121:8080/Aa.jpg会访问http://192.168.50.121:8080/image/Aa.jpg资源,访问aa.jpg会报错
        location ~ /A.?\.jpg {
	  root /usr/share/nginx/html/image/;
	  index index.html;
	}
	
	#不区分大小写,浏览器页面打开http://192.168.50.121:8080/Ba.jpg会访问http://192.168.50.121:8080/image/Bb.jpg资源,访问bb.jpg也能访问到
        location ~* /B.?\.jpg {
	  root /usr/share/nginx/html/image/;
	  index index.html;
	}
	
	#匹配url,如下两个alex。
	#浏览器页面打开http://192.168.50.121:8080/alex会访问http://192.168.50.121:8080/html/alex/index.html
	#浏览器页面打开http://192.168.50.121:8080/alex1会访问http://192.168.50.121:8080/html/alexpc/index.html
	location ^~ /alex {
	  root /usr/share/nginx/html/;
	  index index.html;
	}
        location  /alex1 {
	  root /usr/share/nginx/html/alexpc/;
	  index index.html;
	}

	#文件名后缀
	#浏览器页面打开http://192.168.50.121:8080/mic.jpg会访问http://192.168.50.121:8080/diff/mic.jpg     
	location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js)$ {
	  root /usr/share/nginx/html/diff/;
	  index index.html;
	}
	
	#匹配优先级
	#=,^~,~/~*,/
	#location优先级,(location=) > (location 完整路径) > (location ^~ 路径) > (location ~,~*正则顺序) > (location 部分起始路径) > (/)

        error_page 404 /404.html;
        location = /404.html {
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

3.1 生产用例

3.1.1 生产用例一

vim nginx.conf

user  root;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    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  /var/log/nginx/access.log  main;
    sendfile        on;
    gzip on;
    gzip_types text/css
        application/javascript text/plain application/json application/x-javascript
        application/css application/xml application/xml+rss text/javascript
        application/x-httpd-php image/jpeg image/gif image/png image/x-ms-bmp;
    client_max_body_size 100M;

    upstream ecsSrv {
        server java-ecs:8096;
    }

    upstream ecs-console {
        server java-console:8095;
    }

    server {
        listen       8080;
        server_name  localhost;

        location = / {
            rewrite ^ \$scheme://\$http_host/ecs_console/index.html permanent;
        }
        location ~* \\.(css|js|html|map|gif|jpg|jpeg|png|ico|woff|woff2|svg|mp4|icon)\$ {
            root   /usr/share/nginx/html;
        }

        location ~ /console/ {
            rewrite ^/console/(.*)\$ /\$1 break;
            proxy_read_timeout 240s;
            proxy_pass http://ecs-console;
        }

        location ~ /(ecs|designer|businessobject|datadictionary|distribution|filemanagement|flow|masterdata|rule|treemanager)/ {
            rewrite ^/(ecs|designer|businessobject|datadictionary|distribution|filemanagement|flow|masterdata|rule|treemanager)?/(.*) /\$2 break;
            proxy_read_timeout 240s;
            proxy_pass http://ecsSrv;
        }

        location = /designer/ { index /designer/index.html; }
        location = /ecs-console/ { index /ecs-console/index.html; }
        location = /ecs_console/ { index /ecs_console/index.html; }
        location = /ecs-console-mobile/ { index /ecs-console-mobile/index.html; }
        location = /fssc-web/ { index /fssc-web/index.html; }
        location = /ecs-web/ { index /ecs-web/index.html; }
        location = /ecs-mobile/ { index /ecs-mobile/index.html; }
        location = /ecs-console-mobile-vue/ { index /ecs-console-mobile-vue/index.html; }

        proxy_set_header Host \$host;
        proxy_set_header X-Real-IP \$remote_addr;
        proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
        real_ip_recursive on;
        keepalive_timeout  65;
    }
}

3.1.2 生产用例二

vim conf.d/ecs.conf

	upstream mc-consoleSvr {
		server 192.168.61.187:8072  max_fails=3  fail_timeout=30s;
	}

	upstream mc-ecsXxl {
		server 192.168.61.187:8082 max_fails=3  fail_timeout=30s;
	}

	upstream ecs-cloudplatform-api {
		server 192.168.61.187:8070  max_fails=3  fail_timeout=30s;
	}

	upstream ecs-cloudplatform-mc {
		server 192.168.61.187:8071  max_fails=3  fail_timeout=30s;
	}

server {
	listen 8074;
	server_name 192.168.61.186:8074;
	location = / {
		rewrite ^ /ecs-console/index.html permanent;
	}

	#xxl-job的配置需要放到前面
	location ^~ /xxl-job-admin/ {
		proxy_read_timeout 240s;
		proxy_pass  http://mc-ecsXxl;
	}
	location ^~ /(xxl-job-admin)/(.+)\.(css|js|html|map|gif|jpg|jpeg|png|ico|ttf|woff|apk|ipa)$ {
		proxy_pass  http://mc-ecsXxl;
	}

    location ~ /ecs-cloudplatform-api/ {
        proxy_read_timeout 240s;
        proxy_pass  http://ecs-cloudplatform-api;
    }

	location ~* \.(css|js|html|map|gif|jpg|jpeg|png|ico|svg|mp4)$ {
		root   /usr/share/nginx/html/mc;
	}
	location = /ecs/doc.html {
		proxy_pass  http://mc-consoleSvr;
	}
	location ^~ /ecs/webjars/ {
		proxy_pass  http://mc-consoleSvr;
	}
	location ~ /(console)/ {
		rewrite ^/(console)?/(.*) /$2 break;
		proxy_read_timeout 240s;
		proxy_pass  http://mc-consoleSvr;
	}

    location ~ /ecs-cloudplatform-mc/ {
        proxy_read_timeout 240s;
        proxy_pass  http://ecs-cloudplatform-mc;
    }

    location = /ecs-cloudplatform-mc/ {
        index /ecs-cloudplatform-mc/index.html;
    }
	location = /ecs-console/ {
		index /ecs-console/index.html;
	}
	location = /ecs_console/ {
		index /ecs_console/index.html;
	}
	location = /ecs-console-mobile/ {
		index /ecs-console-mobile/index.html;
	}
	location = /fssc-web/ {
		index /fssc-web/index.html;
	}
	location = /ecs-web/ {
		index /ecs-web/index.html;
	}
	location = /ecs-mobile/ {
		index /ecs-mobile/index.html;
	}
	
	location =/designer/ {
		index /designer/index.html;
	}

	location / {
		rewrite ^/console/(.*)$ /$2 break;
		proxy_read_timeout 240s;
		proxy_pass http://mc-consoleSvr;
	}
	error_page   500 502 503 504  /50x.html;
	location = /50x.html {
		root   /usr/share/nginx/html/mc;
	}

}
posted @ 2022-08-26 18:03  liwenchao1995  阅读(142)  评论(0编辑  收藏  举报