nginx

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > nginx > nginx配置同一个端口转发多项目

nginx如何配置同一个端口转发多个项目

作者:万山寒

这篇文章主要介绍了nginx如何配置同一个端口转发多个项目问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

nginx配置同一个端口转发多项目

nginx一般都是一个转发多个项目,所以需要学会配置这个

方式一

方式二

配置文件

#user  nobody;
worker_processes  8;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  2048;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    proxy_intercept_errors on;
    #tomcat未启动时
    fastcgi_intercept_errors on;

    sendfile        on;
    keepalive_timeout  65;
#   实例一的ip端口配置
    upstream testStream01 {
       server 127.0.0.1:8090;
    }
#   实例二的ip端口配置
    upstream testStream02 {
       server 127.0.0.1:8091;
    }

    server {
        listen       8080;
        server_name  localhost;
		#gzip on;
		#gzip_http_version 1.1;
		#gzip_comp_level 9;
		#gzip_vary on;
		#gzip_types 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;

        location / {
            root   /usr/share/nginx/html/test;
            index  login.html index.htm;
        }
#         实例一的前端页面,主要root 要换成alias
        location /project01 {
#         多个前端项目,需要使用alias
            alias   /usr/share/nginx/html/project01;
            index  login.html index.htm;
        }
#         实例二的前端页面
        location /project02 {
            alias   /usr/share/nginx/html/project02;
            index  login.html index.htm;
        }
#         开放静态资源
#          location  ^/project01/.*\.(jpg|jpeg|gif|png|ico|css|js|pdf|txt)$
#           {
#               alias /usr/share/nginx/html/project01/;
#               proxy_temp_path /usr/share/nginx/html/project01/;
#           }
#        前端请求后端的地址,将会= ip:port/project01 ,所以这里要去掉project01,
#         实例一的后端配置,主要看proxy_pass
		 location  /project01/testServer {

			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header Host $http_host;

			proxy_http_version 1.1;
			proxy_set_header Upgrade $http_upgrade;
			proxy_set_header Connection "upgrade";
			proxy_read_timeout 3600s;
			# proxy_pass "http://testStream01/" 结尾的"/",带上和不带是两种效果,去掉"/",那么路径将接上正则匹配的"/project01/testServer",带上"/",就不会加上。
            proxy_pass  http://testStream01/testServer;
# 			rewrite ^/project01/testServer(.*)$ /testStream01/$1 last;
        }
#         实例二的后端配置,主要看proxy_pass
		location  /project02/testServer {

			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header Host $http_host;

			proxy_http_version 1.1;
			proxy_set_header Upgrade $http_upgrade;
			proxy_set_header Connection "upgrade";
			proxy_read_timeout 3600s;
            proxy_pass  http://testStream02/testServer;
        }

    }


}


总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文