2, nginx的location 详解

	=  :精确匹配(必须全部相等)
    ~  :大小写敏感
    ~* :忽略大小写
    ^~ :只需匹配uri部分,不匹配正则表达式。
    @  :内部服务跳转
	

匹配顺序:
= > ^~ > ~* > /document/ > /

request / :A
request /index.html :B
request /documents/document.html :C
request /images/1.gif :D
request /documents/1.jpg :E


[root@centos7 nginx]# cat nginx.conf

worker_processes 2;
events {
    worker_connections 1024;
}

http {
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    sendfile            on;
    keepalive_timeout   65;
    server {
        listen 80;
        server_name  www.etiantian.org;
        index index.html index.htm;
	location = / {
		return 500;	
	}
	location / {
		return 300;
	}
	location /documents/ {
		return 200;
	}
	location ^~ /images/ {
		return 201;
	}
	location ~* \.(gif|jpg|jpeg)$ {
		return 100;
	}

	}
}
[root@centos7 nginx]#
[root@centos7 nginx]# curl -o /dev/null -s -w "%{http_code}\n" 127.0.0.1
500
[root@centos7 nginx]# curl -o /dev/null -s -w "%{http_code}\n" 127.0.0.1/
500
[root@centos7 nginx]# curl -o /dev/null -s -w "%{http_code}\n" 127.0.0.1/index.html
300
[root@centos7 nginx]# curl -o /dev/null -s -w "%{http_code}\n" 127.0.0.1/documents/document.html
200
[root@centos7 nginx]# curl -o /dev/null -s -w "%{http_code}\n" 127.0.0.1/images/1.gif
201
[root@centos7 nginx]# curl -o /dev/null -s -w "%{http_code}\n" 127.0.0.1/documents/1.jpg
100
[root@centos7 nginx]#  curl -o /dev/null -s -w "%{http_code}\n" 127.0.0.1/www/index.html
300
[root@centos7 nginx]#  curl -o /dev/null -s -w "%{http_code}\n" 127.0.0.1/www/index.html/
300
[root@centos7 nginx]#

  

猜你喜欢

转载自www.cnblogs.com/k8s-pod/p/13377657.html