7. Nginx basic module-virtual site + Location

1 Nginx virtual site

Environment: Implement multiple sites on one server

1.1 Ways to implement virtual hosting

  • Based on IP: different IP
  • Based on port: same IP, different port
  • Based on domain name: same IP, same port, different domain name
基于域名:相同的IP,相同的端口,不同的域名
charset utf-8;
server {
        listen 80;
        server_name baidu.yan.com;					//baidu是主机域
        location / {
                root /html/baidu;
                index index.html;
                access_log /var/log/nginx/baidu.access.log main;
        }
        location /baidu_status {
                stub_status;
        }
}
server {
        listen 80;
        server_name jingdong.yan.com;			//京东是主机域
        location / {
                root /html/jingdong;
                index index.html;
                access_log /var/log/nginx/jingdong.access.log main;
        }
}

Insert picture description here
Insert picture description here

2 Nginx Location

Use Nginx Location to control the path to the website

2.1 Location syntax priority ranking

Match Matching rules priority
= Exact match 1
^~ Start with a string 2
~ Case-sensitive regular matching** 3
~* Case-insensitive regular matching** 4
~! Case-sensitive mismatched regular 5
!~* Case-insensitive mismatched regular 6
/ General matching, any request will be matched** 7

2.2 Configure site verification location priority

server {
        listen 88;
        server_name localhost;
        location / {
                default_type /test/html;
                index index.html;
                return 200  "location /";
                }
    	location = / {
           default_type /test/html;
            index index.html;
            return 200 "location = /";
            }
    	location ~ / {
            default_type /test/html;
            return 200 "location ~ /";
            }
    }

2.3 Location application scenarios

#通用匹配,任何请求都会匹配到
location 	/	{
	}

#严格区分大小写,匹配以.php结尾的都走这个location
location	~	\ .php$  {
		fastcgi_pass http://127.0.0.1:9000
	}

#严格区分大小写,匹配.jsp结尾的都走location
location ~ \ .jsp$ {
	porxy_pass http://127.0.0.1:8080;
}

#不区分大小写匹配,只要用户访问.jpg.gif.png,js,css都走这条location
location	~* 	.*\ .(jgp|gif|png|js|css)$	{
		rewrite	(.*) http://cdn.odlboyedu.com$request_uri;
}s

#不区分大小写匹配
location ~* "\ .(sql|bak|tgz|tar.gz)$" {
	default_type text/html;
	return 403 "启用访问控制成功"
}

Guess you like

Origin blog.csdn.net/weixin_43357497/article/details/113764096