利用nginx解决cookie跨域访问的方法

 更新时间:2018年05月11日 14:25:33   作者:孤王就是朕  
本篇文章主要介绍了利用nginx解决cookie跨域访问的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

一、写在前面

最近需要把阿里云上的四台服务器的项目迁移到客户提供的新的项目中,原来的四台服务器中用到了一级域名和二级域名。比如aaa.abc.com 和bbb.abc.com 和ccc.abc.com。其中aaa.abc.com登录,通过把cookie中的信息setDomain给.abc.com。其他系统可以共享这个cookie。但是新的四台服务器中并没有申请域名,只有四个ip:

192.168.0.1    单点登录服务器

192.168.0.2

192.168.0.3

192.168.0.4

因为每台服务器有两个项目,都用到单点登录,所以通过修改新的共享登录方式花费时间太多,于是在网上搜cookie的跨域登录,尝试了下,在192.168.0.1    单点登录服务器中多次setDomain分别给2、3、4服务器,结果不理想,因为浏览器不允许。后来无意中看到nginx可以通过欺骗的方式共享cookie。于是想到原来公司部署nginx还有这层用法。

二、原来的nginx配置

先说下nginx的安装,这个网上都有很多教程,不在赘述,我是参照于在Linux里安装、启动nginx。需要注意的是./configure后面的各种with,我在配置启动过程遇到了一些问题:

nginx: [emerg] unknown directive "aio" in

加上--with-file-aio 

复制代码 代码如下:
Starting nginx: nginx: [emerg] the INET6 sockets are not supported on this platform in “[::]:80” of the

在后面加上--with-ipv6好使。

安装完成后。主要是nginx.conf的配置

原来服务器的配置nginx.conf:

# For more information on configuration, see:
#  * Official English Documentation: http://nginx.org/en/docs/
#  * Official Russian Documentation: http://nginx.org/ru/docs/

user root;
worker_processes 2;
worker_cpu_affinity 1000 0100;
error_log logs/error.log;
pid logs/nginx.pid;


events {
  worker_connections 2048;
}

http {
  log_format main '$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 main;

  gzip on;
  gzip_min_length 1000;
  gzip_buffers   4 8k;
  gzip_types    text/plain application/javascript application/x-javascript text/css application/xml;

  client_max_body_size 8M;
  client_body_buffer_size 128k;

  sendfile      on;
  tcp_nopush     on;
  tcp_nodelay     on;
  keepalive_timeout  65;
  types_hash_max_size 2048;

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

  connection_pool_size 512;
  aio on;
  open_file_cache max=1000 inactive=20s;

  # Load modular configuration files from the /etc/nginx/conf.d directory.
  # See http://nginx.org/en/docs/ngx_core_module.html#include
  # for more information.
  #  主要配置在这里,nginx.conf配置都是一样
  include /usr/local/nginx/conf/conf.d/*.conf;

  server {
    listen    80 default_server;
    listen [::]:80 ipv6only=on default_server;
    server_name _;
    root     html;

    # Load configuration files for the default server block.
    include /usr/local/nginx/conf/default.d/*.conf;

    location / {
    }

    error_page 404 /404.html;
      location = /40x.html {
    }

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

原来服务器的
conf.d/*.conf的配置是reverse-proxy.conf

server
{
  listen 80;
  server_name m.abc.com.cn;
  location / {
    root  /usr/share/nginx/html/;
    index index.html index.htm;
  }
  location ~ \.(jsp|do)?$ {
    proxy_redirect off;
    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_pass http://localhost:8084;
  }
  if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot") { 
        return 403; 
    }
  access_log /home/logs/nginx/m.abc.com.cn_access.log;
}
 
server
{
  listen 80;
  server_name store.abc.com.cn *.store.abc.com.cn;
  location / {
    proxy_redirect off;
    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_pass http://localhost:8081;
  }
  access_log /home/logs/nginx/store.abc.com.cn_access.log;
}

server
{
  listen 80;
  server_name shopcenter.abc.com.cn;
  location / {
    proxy_redirect off;
    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_pass http://10.45.100.222:8082;
  }
  access_log /home/logs/nginx/shopcenter.abc.com.cn_access.log;
}
 
server
{
  listen 80;
  server_name search.abc.com.cn;
  location / {
    proxy_redirect off;
    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_pass http://10.45.100.68:8083;
  }
  access_log /home/logs/nginx/search.abc.com.cn_access.log;
}

以上配置后,nginx启动后,通过访问不同的域名来访问不同服务器。而因为都有二级域名.abc.com.cn。所以可以共享cookie。

nginx的文件结构为:

三、修改后的nginx配置

主要是reverse-proxy.conf 不同

server
{
  listen 9998;
  server_name 192.168.0.1:9998;
  location /servlets/ {
    proxy_redirect off;
    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_pass http://192.168.0.1:8088;
  }
  location / {

    root  /usr/local/nginx/html/web/;
    index index.html index.htm;
  }
  location ~ \.(jsp|do)?$ {
    proxy_redirect off;
    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_pass http://192.168.0.1:8088;
    
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_read_timeout  700s;
  } 
if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot") { 
        return 403; 
    }
  access_log /usr/local/nginx/logs/www.abc.com.cn_access.log;
}

server
{
  listen 9994;
  server_name 192.168.0.1:9994;
  location / {
   proxy_redirect off;

    root  /usr/local/nginx/html/weixin/;
    index index.html index.htm;
  }
  location ~ \.(jsp|do)?$ {
    proxy_redirect off;
    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_pass http://localhost:8084;
  }
  if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot") { 
        return 403; 
    }
  access_log /usr/local/nginx/logs/m.abc.com.cn_access.log;
}
 
server
{
  listen 9990;
  server_name store.abc.com.cn *.store.abc.com.cn;
  location / {
    proxy_redirect off;
    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_pass http://localhost:8081;
  }
  access_log /usr/local/nginx/logs/store.abc.com.cn_access.log;
}

server
{
  listen 9992;
  server_name 192.168.0.1:9992;
  location / {
    proxy_redirect off;
    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_pass http://192.168.0.2:8082;
  }
  access_log /usr/local/nginx/logs/shopcenter.abc.com.cn_access.log;
}
 
server
{
  listen 9993;
  server_name 192.168.0.1:9993;
  location / {
    proxy_redirect off;
    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_pass http://192.168.0.3:8083;
  }
  access_log /usr/local/nginx/logs/search.abc.com.cn_access.log;
}

 这样就可以把192.168.0.1:9998 当做单点服务器,登录后的domain都为192.168.0.1 。其他的0.2、0.3都可以通过192.168.0.1nginx和单点服务器的不同端口访问,那么就可以共享这个0.1的域名了。

四、最后

好吧,可能描述的不是那么清楚,有点乱。我所做的工作就是把原来的nginx配置中的端口和域名改成新服务器中的唯一一个ip把这个ip当做那个域名,不同端口对应不同二级域名。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Nginx路径匹配规则小结

    Nginx路径匹配规则小结

    本文主要介绍了Nginx路径匹配规则小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • 浅析Nginx配置文件中的变量的编写使用

    浅析Nginx配置文件中的变量的编写使用

    这篇文章主要介绍了Nginx配置文件中的变量的编写使用,包括从常用的rewrite等方面来深入变量的相关定义,需要的朋友可以参考下
    2016-01-01
  • nginx location/区别详解

    nginx location/区别详解

    本文主要介绍了nginx location/区别详解,主要介绍了8中不同的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04
  • Nginx优化设计方案小结

    Nginx优化设计方案小结

    本文主要介绍了Nginx优化设计方案小结,帮助大家在nginx的使用和优化中提供一个参考的方向,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • nginx访问日志并删除指定天数前的日志记录配置方法

    nginx访问日志并删除指定天数前的日志记录配置方法

    这篇文章主要介绍了nginx访问日志并删除指定天数前的日志记录配置方法,需要的朋友可以参考下
    2014-03-03
  • 通过Nginx配置实现外网访问内网数据库的操作指南

    通过Nginx配置实现外网访问内网数据库的操作指南

    项目开发部署中经常会遇到MySQL或Oracle数据库安装在内网,而我们的应用服务只能部署在外网,如果实现外网服务访问连接内网的数据库呢?本次介绍如何通过Nginx配置实现外网访问内网数据库,需要的朋友可以参考下
    2023-10-10
  • nginx修改配置文件不生效解决方案

    nginx修改配置文件不生效解决方案

    当你修改了Nginx的配置文件后,需要重新加载配置文件,使得修改生效,这篇文章主要给大家介绍了关于nginx修改配置文件不生效的解决方案,需要的朋友可以参考下
    2023-08-08
  • Nginx更改conf配置文件的代码详解

    Nginx更改conf配置文件的代码详解

    本文主要介绍了Nginx如何更改conf配置文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作有一定的参考学习价值,需要的朋友们下面跟着小编来一起来学习吧
    2024-02-02
  • Nginx 代理解决跨域问题多种情况分析

    Nginx 代理解决跨域问题多种情况分析

    这篇文章主要介绍了Nginx 代理解决跨域问题分析,通过用网站8080访问Nginx代理后的接口地址,报错分为多种情况,每种情况给大家详细分析,感兴趣的朋友一起看看吧
    2022-01-01
  • 详解使用ChatGPT解决Nginx反向代理的问题

    详解使用ChatGPT解决Nginx反向代理的问题

    这篇文章主要为大家介绍了使用ChatGPT解决Nginx反向代理的问题详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03

最新评论