有两个.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 }

使用Nginx在80端口上代理多个.NET CORE网站的更多相关文章

  1. nginx把两个不同的网站挂到同一台服务器的80端口上

    本文主要分享如果利用nginx把两个不同的网站挂到同一台服务器的80端口上 最终效果 例如:www.manyjar.com (web服务器是8080端口)和ishenghuo.manyjar.com ...

  2. 解决启动nginx时报80端口被占用的问题

    如何解决启动nginx时报80端口被占用 最近公司的的一个服务器上需要部署多个项目,但80端口只有一个,所有只有使用Nginx来代理,当访问域名时就可以自动 转到IP:端口号,而不需要在域名后面加端口 ...

  3. 如何在Web服务器80端口上开启SSH服务

    本文所讨论的网络端口复用并非指网络编程中采用SO_REUSEADDR选项的 Socket Bind 复用.它更像是一个带特定路由功能的端口转发工具,在应用层实现. 背景 笔者所处网络中防火墙只开放了一 ...

  4. Win7下nginx默认80端口被System占用,造成nginx启动报错的解决方案

    Win7下nginx默认80端口被System占用,造成nginx启动报错的解决方案   在win7 32位旗舰版下,启动1.0.8版本nginx,显示如下错误:  [plain] 2012/04/0 ...

  5. 如何用nginx在本地把9000端口转发到80端口上

    起因看到一个用java写的轻博客,于是就兴致冲冲的试用一下.由于是lnmp的环境,Nginx占用了80端口,新博客只能用其他的端口,这里选择了9000端口,本地测试没问题.总不能访问了域名然后在加上端 ...

  6. docker上部署nginx容器80端口自动转443端口

    拉去nginx镜像 # docker pull nginx 运行nginx容器config用于拷贝nginx配置文件 # docker run --name nginxconfig -d docker ...

  7. Web、WCF和WS通过Nginx共享80端口

    团队中的一个Web项目面对的用户网络环境多是在严格的防火墙安全条件下,通常只开放一些标准的端口如80,21等. 上线初期,因忽略了这个问题,除了Web应用是以80端口提供访问外,WCF和WS是以其他端 ...

  8. apache 80 端口 反向代理 tomcat 8080端口

    最近有个jsp的项目要放到服务上,但服务器上已经有了XAMPP(apache + mysql + php), 已占用了80端口.但http默认是访问80端口的. 先把tomcat 环境搭建起来, 发现 ...

  9. Win7下,nginx默认80端口被System占用,造成nginx启动报错

    在win7 32位旗舰版下,启动1.0.8版本nginx,显示如下错误: 2012/04/02 13:55:59 [emerg] 7864#2376: bind() to 0.0.0.0:80 fai ...

  10. nginx的80端口跳转到443

    然后在80端口设置http跳转到https 在80端口location中加入rewrite ^(.*)$ https://$host$1 permanent; 如图,然后输入命令service ngi ...

随机推荐

  1. uiautomator2使用方法

    一.设备连接 1.usb单设备连接 d = u2.connect() 2.usb多设备连接 d = u2.connect("90bf8faf") # 多台设备填写device即可 ...

  2. FE知识点(硕哥)

    目录 前传: 1.typeof和类型转换 正文: 1.作用域.作用域链([[scope]]) 2.立即执行函数 3.闭包 4.对象.包装类 5.原型原型链 6.call.apply 7.继承模式.命名 ...

  3. 【iOS逆向与安全】iOS远程大师:通过H5后台远程查看和协助iPhone设备

    前言 在移动设备测试和远程协助的过程中,能够远程查看和协助iPhone设备是一项非常实用的功能.为了解决这一需求,我开发了一款名为iOS远程大师的产品,允许用户通过H5后台界面查看和协助越狱或非越狱的 ...

  4. hashMap添加key重复时返回值的形式

    hashMap添加key重复 System.out.println(map.put(1, 0)); // null System.out.println(map.put(1, 1)); // 0 Sy ...

  5. java学习之旅(day.08)

    类与对象的关系 类是一种抽象的数据类型,是对某一类事物的描述,但并不代表具体的事物,如动物与狗的关系,类描述的是某一类事物具备的共同特点 对象是抽象概念的具体实例 能够展现出功能,体现出特点的是具体的 ...

  6. k8s——deployment

    创建deployment [root@master deploy]# kubectl create deploy nginx-deploy --image=nginx:1.7.9 deployment ...

  7. 《python核心编程《第二版》》笔记章节索引

    本文章作为我的其它一系列关于<python核心编程<第二版>>的笔记的文章索引. 第一章:Python-快速入门:https://www.cnblogs.com/mrlayfo ...

  8. THUWC 2024 游记

    其实这个游记也没啥好写的-- day 0 上午做动车两个小时到重庆,路上玩了 1.5h 的星露谷. 下午去巴蜀中学报道试机,系统是 Ubuntu Jammy,大大的好评,只是桌面是 Xubuntu/X ...

  9. LeetCode 207. Course Schedule 课程表 (C++/Java)

    题目: There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have pr ...

  10. C# .NET 生成国密私钥公钥对

    使用的工具类: using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.GM; using Org.BouncyCastle.Asn1.X9; ...