文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

怎么在nginx中解决cookie跨域访问问题

2023-06-08 00:49

关注

怎么在nginx中解决cookie跨域访问问题?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

1

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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

# 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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

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中解决cookie跨域访问问题

三、修改后的nginx配置

主要是reverse-proxy.conf 不同

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

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的域名了。

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注编程网行业资讯频道,感谢您对编程网的支持。

http://www.cnblogs.com/minzhousblogs/p/9023391.html

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯