nginx代理配置及用nginx解决跨域问题

本文介绍如何使用Nginx代理实现三个服务的单点登录,避免重复登录,同时分享Nginx配置解决跨域问题的方法。

之前一直听说nginx的代理模式,但一直没有实际接触,但是最近项目需要将三个服务公用一个登陆系统,也就是说只要在一个服务器里登陆,其他服务器不需要再登陆了,有人说用单点登陆,但是实际情况用单点登陆改的代码很多,通过大神指点可以用nginx代理,可以共用同一个cookie从而实现。
不同的端口所保存的cookie是不能通用的,只有通过nginx代理。原理就是当三个服务端口为8080,8081,8082,而配置nginx端口为88;看配置

server {
        listen       88;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /server2/{
            proxy_pass   http://127.0.0.1:8082/server2/;
        } 
        location /server1/ {
		proxy_pass   http://127.0.0.1:8081/server1/;
        }
 		location /server/ {
		proxy_pass   http://127.0.0.1:8080/server/;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   D:\wwwroot;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

需要注意的是localhost后面需要固定服务的地址前缀,server1,这样访问时可以直接访问:http://127.0.0.1:88/servce1/login.html,这样就能直接访问到端口为8081的服务。大家可以尝试配置一下

在附上nginx解决跨域问题的配置

http {
    include       mime.types;
    default_type  application/octet-stream;
	
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Headers X-Requested-With;
    add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
    .
    .
    .
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值