Nginx使用80端口反向代理多个项目(包含Java、IIS项目),作为静态资源服务器

总体配置文件

#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;


#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    #access_log  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;

   #HTTP服务器
   server {
        #监听80端口,80端口是知名端口号,用于HTTP协议
        listen       80;
        
        #定义使用www.xx.com访问
        server_name  www.mkwb1.org.cn;
        
        
        #编码格式
        charset utf-8;
        
        #代理配置参数
        proxy_connect_timeout 180;
        proxy_send_timeout 180;
        proxy_read_timeout 180;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarder-For $remote_addr;
       
       
 
		#使用location对不同请求做相应处理
		#Java项目1
        location /portal/ {
            proxy_pass http://localhost:8080;
        }
		
		#Java项目2
		location /manage/ {
            proxy_pass http://localhost:8888;
        }
		
		# 本地存放图片位置
		location /images/{
			root  c:/media;

		}
		# 本地MP3位置
		location /MP3/{
			root  c:/media;

		}
		# “=”精确匹配
		location =/{
            proxy_pass http://localhost:8080/portal/home;
        }
		
        
    }


	#IIS服务根据域名区分代理
	#IIS服务1
	server {
        listen       80;
        server_name  zuul1.com;
 
        location / {
			proxy_pass   http://zuul1.com:8083;
        }

    }
	#IIS服务2
    server {
        listen       80;
        server_name  zuul.com;
		
        location / {
			proxy_pass   http://zuul.com:8081;
        }
    }
}

简单介绍下本文使用的location匹配规则(个人理解)

  1. /xxxx/:匹配以“xxx”下的所有uri
  2. =/:精确匹配proxy_pass http://localhost:8080/portal/home; 该段代码内url跳转的界面,我用它跳转到首页。

注意

  1. 一个location下不能有相同的匹配规则
  2. 每个server下的server_name不可重复

猜你喜欢

转载自blog.csdn.net/qwqw3333333/article/details/89599124