nginx 本地配置(解决跨域问题)

前端做跨域本身就是扯淡的事情。后台人员不配合说什么都是白搭。索性整理了一下心得,(可以直接部署,配置改一下)话不多说上代码:

  1 #user  nobody;
  2 worker_processes  1;
  3 
  4 #error_log  logs/error.log;
  5 #error_log  logs/error.log  notice;
  6 #error_log  logs/error.log  info;
  7 
  8 #pid        logs/nginx.pid;
  9 
 10 
 11 events {
 12     worker_connections  1024;
 13 }
 14 
 15 
 16 http {
 17     include       mime.types;
 18     default_type  application/octet-stream;
 19 
 20     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 21     #                  '$status $body_bytes_sent "$http_referer" '
 22     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 23 
 24     #access_log  logs/access.log  main;
 25 
 26     sendfile        on;
 27     #tcp_nopush     on;
 28 
 29     #keepalive_timeout  0;
 30     keepalive_timeout  65;
 31 
 32     #gzip  on;
 33 
 34     upstream ma {
 35         server t.manage.xxxxx.com;//测试服地址
 36     }
 37 
 38     upstream permission {
 39         server t.manage.xxxxx.com;//测试服地址
 40     }
 41  54 
 55     server {
 56         listen       80;
 57         server_name  t.test.cn; //此处是改变本地的host的配置(好处就是可以把这个配置文件直接部署到线上直接使用,改个名字就行),同时也是本地访问的域名
 58 
 59         #charset koi8-r;
 60 
 61         #access_log  logs/host.access.log  main;
 62 
 63         location /permission/{
 64             proxy_pass http://permission; 
 65             proxy_pass_header Server;
 66             proxy_set_header Host t.manage.xxx.com;//这是解决服务端地址冲突的问题
 67             proxy_set_header X-Real-IP $remote_addr;
 68             proxy_set_header X-Scheme $scheme;
 69             proxy_connect_timeout  60s; 
 70             client_max_body_size  30m;
 71         }
 72 
 73         location /ma/{
 74             proxy_pass http://ma; 
 75             proxy_pass_header Server;
 76             proxy_set_header Host t.manage.xxxx.com;//同上
 77             # proxy_set_header Host $http_host;
 78             proxy_set_header X-Real-IP $remote_addr;
 79             proxy_set_header X-Scheme $scheme;
 80             proxy_connect_timeout  60s; 
 81             client_max_body_size  30m;
 82         }
 83 
 84         location / {
 85             root E:/gsthj/manage-web;//地址指向要更改
 86             index  login.html;
 87         }
 88     }
 89 
 90 141 
142     
143         #error_page  404              /404.html;
144 
145         # redirect server error pages to the static page /50x.html
146         #
147         error_page   500 502 503 504  /50x.html;
148         location = /50x.html {
149             root   html;
150         }
151 
152     }
153 
154     
155 }

本地host

 1 # Copyright (c) 1993-2009 Microsoft Corp.
 2 #
 3 # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
 4 #
 5 # This file contains the mappings of IP addresses to host names. Each
 6 # entry should be kept on an individual line. The IP address should
 7 # be placed in the first column followed by the corresponding host name.
 8 # The IP address and the host name should be separated by at least one
 9 # space.
10 #
11 # Additionally, comments (such as these) may be inserted on individual
12 # lines or following the machine name denoted by a '#' symbol.
13 #
14 # For example:
15 #
16 #      102.54.94.97     rhino.acme.com          # source server
17 #       38.25.63.10     x.acme.com              # x client host
18 
19 # localhost name resolution is handled within DNS itself.
20 #    127.0.0.1       localhost
21 #    ::1             localhost
22 
23     127.0.0.1       t.test.cn
24     127.0.0.1       t1.test.cn

基本的部署就是很简单的方式,更灵活的用法就要靠各位大佬自己了。

完结撒花****************************

posted @ 2021-04-09 16:39  11个条子  阅读(462)  评论(0编辑  收藏  举报