系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 系统教程 > Linux教程 > 详细页面

使用Nginx在80端口上代理多个.NET CORE网站

时间:2020-09-28来源:www.pcxitongcheng.com作者:电脑系统城

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

相关信息

  • linux 文件权限怎么解析

    常用权限linux系统内有档案有三种身份 u:拥有者 g:群组 o:其他人这些身份对于文档常用的有下面权限:r:读权限,用户可以读取文档的内容,如用cat,more查看w:写权限,用户可以编辑文档x...

    2024-07-07

  • 如何使用WPSeku找出 WordPress 安全问题?

    然而,如果我们遵循通常的 WordPress 最佳实践,这些安全问题可以避免。在本篇中,我们会向你展示如何使用 WPSeku,一个 Linux 中的 WordPress 漏洞扫描器,它可以被用来找出你安装...

    2024-07-03

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载