有两个.NET CORE3.1网站部署在CentOS7上(内网IP是192.168.2.32),现在想实现访问http://192.168.2.32时访问A网站,访问http://192.168.2.32/bmd/ 时访问的是B网站。
在Nginx里配置两个location可以实现,但会导致B网站的样式和js丢失(B网站页面引用js和css的方式是/css/*和/js/*)。
经过摸索,通过在location /中配置$http_referer来进行跳转,即可完美实现A、B两个网站独立访问。
具体配置如下:
1 # For more@H_502_14@ information on configuration,see: @H_502_14@ 2 # * Official English Documentation: http:@H_502_14@//@H_502_14@Nginx.org/en/docs/ @H_502_14@ 3 # * Official Russian Documentation: http:@H_502_14@Nginx.org/ru/docs/ @H_502_14@ 4 @H_502_14@ 5 @H_502_14@user root; @H_502_14@ 6 @H_502_14@worker_processes auto; @H_502_14@ 7 error_log /var/log/Nginx/@H_502_14@error.log; @H_502_14@ 8 pid /run/@H_502_14@Nginx.pid; @H_502_14@ 9 @H_502_14@ 10 # Load dynamic modules. See /usr/share/doc/Nginx/@H_502_14@README.dynamic. @H_502_14@ 11 include /usr/share/Nginx/modules@H_502_14@/*@H_502_14@.conf; @H_502_14@ 12 @H_502_14@ 13 @H_502_14@events { @H_502_14@ 14 @H_502_14@ worker_connections 1024; @H_502_14@ 15 @H_502_14@} @H_502_14@ 16 @H_502_14@ 17 @H_502_14@http { @H_502_14@ 18 @H_502_14@ log_format main '$remote_addr - $remote_user [$time_local] "$request" ' @H_502_14@ 19 @H_502_14@ '$status $body_bytes_sent "$http_referer" ' @H_502_14@ 20 @H_502_14@ '"$http_user_agent" "$http_x_forwarded_for"'; @H_502_14@ 21 @H_502_14@ 22 @H_502_14@ access_log /var/log/Nginx/access.log main; @H_502_14@ 23 @H_502_14@ gzip on; @H_502_14@ 24 @H_502_14@ 25 @H_502_14@ sendfile on; @H_502_14@ 26 @H_502_14@ tcp_nopush on; @H_502_14@ 27 @H_502_14@ tcp_nodelay on; @H_502_14@ 28 @H_502_14@ keepalive_timeout 65; @H_502_14@ 29 @H_502_14@ types_hash_max_size 2048; @H_502_14@ 30 @H_502_14@ 31 @H_502_14@ include /etc/Nginx/mime.types; @H_502_14@ 32 @H_502_14@ default_type application/octet-stream; @H_502_14@ 33 @H_502_14@ 34 @H_502_14@ # Load modular configuration files from the /etc/Nginx/conf.d directory. @H_502_14@ 35 @H_502_14@ # See http://Nginx.org/en/docs/ngx_core_module.html@H_502_14@#include @H_502_14@ 36 @H_502_14@ # for more information. @H_502_14@ 37 @H_502_14@ include /etc/Nginx/conf.d/*.conf; @H_502_14@ 38 @H_502_14@ 39 @H_502_14@ server { @H_502_14@ 40 @H_502_14@ listen 80; @H_502_14@ 41 @H_502_14@ listen [::]:80; @H_502_14@ 42 @H_502_14@ server_name web; @H_502_14@ 43 @H_502_14@ #root /usr/share/Nginx/html; @H_502_14@ 44 @H_502_14@ 45 @H_502_14@ # Load configuration files for the default server block. @H_502_14@ 46 @H_502_14@ include /etc/Nginx/default.d/*.conf; @H_502_14@ 47 @H_502_14@ 48 @H_502_14@ location / { @H_502_14@ 49 @H_502_14@ proxy_http_version 1.1; @H_502_14@ 50 @H_502_14@ proxy_set_header Upgrade $http_upgrade; @H_502_14@ 51 @H_502_14@ proxy_set_header Connection keep-alive; @H_502_14@ 52 @H_502_14@ proxy_set_header Host $host; @H_502_14@ 53 @H_502_14@ proxy_set_header X-Real-IP $remote_addr; @H_502_14@ 54 @H_502_14@ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; @H_502_14@ 55 @H_502_14@ proxy_cache_bypass $http_upgrade; @H_502_14@ 56 @H_502_14@ proxy_set_header X-Nginx-Proxy true; @H_502_14@ 57 @H_502_14@ if ($http_referer ~ 'bmd') @H_502_14@ 58 @H_502_14@ { @H_502_14@ 59 @H_502_14@ #permanent代表301永久跳转,redirect为302临时跳转,这里的配置是核心,凡是bmd的前缀都带上bmd,从而解决了/css和/js引用404的问题 @H_502_14@ 60 @H_502_14@ rewrite ^/(.*)$ http://$host/bmd/$1 permanent; @H_502_14@ 61 @H_502_14@ } @H_502_14@ 62 @H_502_14@ proxy_pass http://127.0.0.1@H_502_14@:5000/; @H_502_14@ 63 @H_502_14@ } @H_502_14@ 64 @H_502_14@ location ^~/bmd/ { @H_502_14@ 65 @H_502_14@ root /usr/local/whitelist; @H_502_14@ 66 @H_502_14@ 67 @H_502_14@ 68 @H_502_14@ 69 @H_502_14@ 70 @H_502_14@ 71 @H_502_14@ 72 @H_502_14@ 73 @H_502_14@ 74 @H_502_14@ #rewrite ^/bmd/(.*)$ /$1 break; @H_502_14@ 75 @H_502_14@ #proxy_redirect ~^http://192.168.2.32/bmd/@H_502_14@(.*)$ @H_502_14@:5001/$1; @H_502_14@ 76 @H_502_14@:5001/; @H_502_14@ 77 @H_502_14@ 78 @H_502_14@ location /Nginxstatus { @H_502_14@ 79 @H_502_14@ stub_status on; @H_502_14@ 80 @H_502_14@ access_log /usr/local/Nginx/logs/status.log; @H_502_14@ 81 @H_502_14@ auth_basic "NginxStatus"; @H_502_14@ 82 @H_502_14@ 83 @H_502_14@ 84 @H_502_14@ error_page 404 /404.html; @H_502_14@ 85 @H_502_14@ location = /40x.html { @H_502_14@ 86 @H_502_14@ 87 @H_502_14@ 88 @H_502_14@ error_page 500 502 503 504 /50x.html; @H_502_14@ 89 @H_502_14@ location = /50x.html { @H_502_14@ 90 @H_502_14@ 91 @H_502_14@ } @H_502_14@ 92 @H_502_14@ 93 @H_502_14@ 94 @H_502_14@ listen 8000; @H_502_14@ 95 @H_502_14@ listen [::]:8000; @H_502_14@ 96 @H_502_14@ server_name api; @H_502_14@ 97 @H_502_14@ 98 @H_502_14@ 99 @H_502_14@100 @H_502_14@101 @H_502_14@102 @H_502_14@ location /api/v1 { @H_502_14@103 @H_502_14@:5003; @H_502_14@104 @H_502_14@105 @H_502_14@106 @H_502_14@107 @H_502_14@108 @H_502_14@109 @H_502_14@110 @H_502_14@111 @H_502_14@112 @H_502_14@113 @H_502_14@114 @H_502_14@115 @H_502_14@116 @H_502_14@117 @H_502_14@118 @H_502_14@119 @H_502_14@120 @H_502_14@121 @H_502_14@}