有两个.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. 在Docker内部使用gdb调试器报错-Operation not permitted

    在docker内部使用gdb调试时刻遇到了gdb如下报错信息: warning: Error disabling address space randomization: Operation not ...

  2. 解锁高效创新:IPD策略如何重塑产品开发流程

    IPD(集成产品开发)涵盖了产品从创意提出到研发.生产.运营等,包含了产品开发到营销运营的整个过程.围绕产品(或项目)生命周期的过程的管理模式,是一套生产流程,更是时下国际先进的管理体系.IPD(集成 ...

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

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

  4. pod(二):创建包含多个容器的pod(sidecar)

    目录 一.系统环境 二.前言 三.创建包含多个容器的pod 3.1 环境介绍 3.2 在一个pod里创建多个容器 一.系统环境 服务器版本 docker软件版本 CPU架构 CentOS Linux ...

  5. linux下date命令设置时间的输出格式和修改时间

    目录 一.关于linux下的时间 二.linux下使用date命令设置时间的输出格式 三.修改linux时间 3.1 在可以访问Internet情况下修改时间 3.2 在无法访问Internet情况下 ...

  6. 鸿蒙HarmonyOS实战-Stage模型(应用上下文Context)

    前言 应用上下文(Context)是应用程序的全局信息的接口.它是一个抽象类,提供了访问应用程序环境的方法和资源的方法.应用上下文可以用于获取应用程序的资源.启动Activity.发送广播等.每个应用 ...

  7. C# winfrom 局域网版多人成语接龙(一)

    在学习 springjdbc+c3p0时做了一个数据库版的获得给定词汇的成语接龙,这个做了之后,我突然就想做一个可供多人游戏的成语接龙游戏,由于自己根本不熟悉java的图形界面开发,感觉没有winfo ...

  8. Docker 启动 Redis 就停止解决方案(2022-3)

    启动命令如下: docker run -itd \ -p 6379:6379 \ --name myredis \ -v /home/redis/redis.conf:/etc/redis/redis ...

  9. RTOS官方文档学习

    任务与协程 区别 一个程序可以只有任务.只有协程.二者都有,但不可以通过队列/信号量互相传递数据 任务特点 任务之间可以互相独立 每个任务分配自己的堆栈,提高了RAM使用率 操作简单.按优先级抢占式执 ...

  10. 基于ReAct机制的AI Agent

    当前,在各个大厂纷纷卷LLM的情况下,各自都借助自己的LLM推出了自己的AI Agent,比如字节的Coze,百度的千帆等,还有开源的Dify. 你是否想知道其中的原理?是否想过自己如何实现一套AI ...