nginx配置子域名跳转到后端请求

项目情况简介:公司业务调整,新建子公司,专门做理财相关业务,对应需要分离出licai子站,但开发人员有限,最终定了主站和子站共用源代码和TOMCAT服务器,在NGINX上配置不同域名跳转到对应后端请求。


环境简介:全站https,两个SLB,一个SLB对应uat.mydomain.com,另外一个SLB对应子站的licai.mydomain.com。

服务器采用了NGINX做静态缓存和反向代理。新建子域名是直接配置DNS,指向对应的SLB.




对应的NGINX配置如下:

worker_processes  1;

events {
    use epoll;
    worker_connections  1024;
}


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

    log_format  uat.mydomain.com  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  uat.mydomain.com;

    sendfile        on;
    keepalive_timeout  600;

    upstream uat.mydomain.com { 
        server 127.0.0.1:8080; #可以根据需要修改
    } 

    upstream licai.mydomain.com { 
        server 127.0.0.1:8080; #可以根据需要修改
    } 
	
    #用于共享文件
    server {
        listen       82;
        server_name  localhost;
        location / {
            root   /mnt/attachments;
            index  index.html index.htm;
        }
	}	
 
     server {
        listen      80;
        server_name  uat.mydomain.com licai.mydomain.com;	#80端口支持的域名
        client_header_buffer_size 128k;
        large_client_header_buffers 4 128k;
		#add_header Set-Cookie loginSessionHttp;
        location / {
            root   html;
            index  index.html index.htm;
	        rewrite ^(.*) https://$host$1 permanent;	#全站https
            proxy_buffer_size 64k;
            proxy_buffers   32 32k;
            proxy_busy_buffers_size 128k;
			proxy_set_header       Host $host;
            proxy_set_header  X-Real-IP  $remote_addr;
			proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header X-Forwarded-Proto  $scheme;
            client_max_body_size 50m;
            proxy_connect_timeout 360;
			port_in_redirect off;
			real_ip_header X-Forwarded-For;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    } 
	
    server {
        listen      443;
        server_name  uat.mydomain.com;
		client_header_buffer_size 128k;
        large_client_header_buffers 4 128k;
		add_header Set-Cookie loginSessionHttps;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_buffers   32 32k;
            proxy_busy_buffers_size 128k;
            proxy_set_header       Host $host;
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto  $scheme;
            client_max_body_size 50m;
            proxy_connect_timeout 360;
            port_in_redirect off;
            real_ip_header X-Forwarded-For; 
            proxy_pass http://uat.mydomain.com/;
         }

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

    }

    server {
        listen      445;
        server_name  licai.mydomain.com;
		client_header_buffer_size 128k;
        large_client_header_buffers 4 128k;
		add_header Set-Cookie loginSessionHttps;
	    port_in_redirect off; #防止跳转的时候带了端口号,会导致404
		
	    #映射图片和资源文件请求和主站一致
	    location ~ \.(css|js|gif|jpg|swf|png|htm|ico)$ {
		   proxy_pass http://licai.mydomain.com;
        }
		
	#如下几条为了子站和主站公用后端代码
	location ~* /*financing* {
            proxy_pass http://licai.mydomain.com;
        }		

	location ~* /*login/login* {
            proxy_pass http://licai.mydomain.com;
        }
		
	location ~* /*login/logout* {
            proxy_pass http://licai.mydomain.com;
        }		
		
	location ~* /*userManage* {
            proxy_pass http://licai.mydomain.com;
        }		

	location ~* /*sendSmsCode* {
            proxy_pass http://licai.mydomain.com;
        }				

	#映射子域名到对应的后端请求
        location / {
            root   html;
            index  index.html index.htm;
            proxy_buffers   32 32k;
            proxy_busy_buffers_size 128k;
            proxy_set_header       Host $host;
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto  $scheme;
            client_max_body_size 50m;
            proxy_connect_timeout 360;
            port_in_redirect off;
            real_ip_header X-Forwarded-For; 
            proxy_pass http://licai.mydomain.com/financing/; #映射子域名到对应的后端请求
         }

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

    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值