用nginx实现前后端分离的项目的前后端访问

现在几乎都是前后端分析的项目,在开发和线上一般都会使用nginx做代理来实现跨域和负债均衡。nginx安装前面已经讲过,现在写写如何配置,前端技术为vue。

server {
      listen 80;
      server_name zq.shidebin.com;

      location / {
        root /var/www/html/yanwei/zq-arbitrate-client;
        index index.html index.htm;
      }
      location /web/ {
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $host;
          proxy_set_header accept-encodeing 'gzip, deflate';
          proxy_set_header content-type 'application/json';
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header authorization $http_authorization;
          proxy_set_header accept '*/*';
          proxy_set_header x-bce-date $http_x_bce_date;
          proxy_connect_timeout 300;
          proxy_send_timeout 300;
          proxy_read_timeout 300;
          proxy_pass http://192.168.2.88:8081/web/;
      }
    }
    server {
        listen 80;
      server_name private.shidebin.com;

      location / {
        root /var/www/html/yanwei/private-arbitrate-client;
        index index.html index.htm;
      }
      location /web/ {
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $host;
          proxy_set_header accept-encodeing 'gzip, deflate';
          proxy_set_header content-type 'application/json';
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header authorization $http_authorization;
          proxy_set_header accept '*/*';
          proxy_set_header x-bce-date $http_x_bce_date;
          proxy_connect_timeout 300;
          proxy_send_timeout 300;
          proxy_read_timeout 300;
          proxy_pass http://192.168.2.88:8080/web/;
      }
}
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
前端代码在路径/var/www/html/yanwei/private-arbitrate-client中。相当于原本访问nginx欢迎页的想在访问前端页面,然后前端到后端的跳转url被nginx拦截在转发。
加上负载均衡:

upstream private {
     server   192.168.2.88:8080 weight=1 max_fails=2 fail_timeout=30s;
     server   192.168.1.252i3:8280 weight=1 max_fails=2 fail_timeout=30s;
    }
    upstream zq {
     server   192.168.2.88:8081 weight=1 max_fails=2 fail_timeout=30s;
     server   127.0.0.1:8091 weight=1 max_fails=2 fail_timeout=30s;
    }
    #gzip  on;
    server {
      listen 80;
      server_name zq.shidebin.com;

      location / {
        root /var/www/html/yanwei/zq-arbitrate-client;
        index index.html index.htm;
      }
      location /web/ {
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $host;
          proxy_set_header accept-encodeing 'gzip, deflate';
          proxy_set_header content-type 'application/json';
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header authorization $http_authorization;
          proxy_set_header accept '*/*';
          proxy_set_header x-bce-date $http_x_bce_date;
          proxy_connect_timeout 300;
          proxy_send_timeout 300;
          proxy_read_timeout 300;
          proxy_pass http://zq;
      }
    }
    server {
        listen 80;
      server_name private.shidebin.com;

      location / {
        root /var/www/html/yanwei/private-arbitrate-client;
        index index.html index.htm;
      }
      location /web/ {
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $host;
          proxy_set_header accept-encodeing 'gzip, deflate';
          proxy_set_header content-type 'application/json';
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header authorization $http_authorization;
          proxy_set_header accept '*/*';
          proxy_set_header x-bce-date $http_x_bce_date;
          proxy_connect_timeout 300;
          proxy_send_timeout 300;
          proxy_read_timeout 300;
          proxy_pass http://private;
      }

————————————————
版权声明:本文为CSDN博主「星坠竹空」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/shidebin/article/details/86512313

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值