有两个.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. Linux部署Apache 网站服务器(httpd服务)

    一.项目导入: 某学院组建了校园网,建设了学院网站.现需要架设Web服务器来为学院网站安家,同时在网站上传和更新时,需要用到文件上传和下载,因此还要架设FTP服务器,为学院内部和互联网用户提供WWW. ...

  2. springboot中quartz定时器的postgresql建表语句

    建表语句如下: DROP TABLE IF EXISTS QRTZ_FIRED_TRIGGERS; DROP TABLE IF EXISTS QRTZ_PAUSED_TRIGGER_GRPS; DRO ...

  3. openstack以后需要研究一下的知识

    1. openvt是一个用于在虚拟终端上启动程序的命令行工具.它允许用户在一个新的虚拟终端(VT)上启动一个程序,并将标准输入.输出和错误输出定向到该终端. openvt的用法如下: 打开一个虚拟终端 ...

  4. docker --link容器互联

    目录 一.系统环境 二.docker容器互联概述 2.1 docker容器互联的三种方式 2.2 docker --link使用注意事项 2.3 docker --link原理 三.docker容器互 ...

  5. linux用户管理:创建用户,删除用户,管理用户,用户配置

    目录 一.关于用户 二.用户的三种类型 三.与用户有关的配置文件详解 四.创建用户 五.设置用户密码 六.删除用户 七.用户密码时效管理 八.查看用户相关信息的命令 九.修改用户基本信息 十.管理用户 ...

  6. CSS——3D转换

  7. NET工控,上位机,Modbus485网口/串口通讯(鸣志步进电机,鸣志伺服电机,松下伺服电机,华庆军继电器模块)

    先上两个通用Modbus帮助类,下面这个是多线程不安全版,在多线程多电机同一端口通信下,可能造成步进电机丢步或者输出口无响应等,还有个多线程安全版,只是基于这个不安全版加上了LOCK,THIS usi ...

  8. 使用C代码实现读取文件中的hex字符串,转换为字节数组

    举例说明: ptp.txt文件中的内容为: 7a7ac0a8c80100000000003388f70002002c000004000000000000000000000000000000000000 ...

  9. c# winfrom DataGridView 动态UI下载功能(内含GIF图) || 循环可变化的集合 数组 datatable 等

    Gif演示 分解步骤 1,使用组件DataGridView 2,使用DataSource来控制表格展示的数据来源(注意:来源需要是DataTable类型) 3,需要用到异步线程.如果是不控制数据源的话 ...

  10. react兄弟之间通信

    写入组件 import React, { Component } from 'react'//下面二个就是兄弟关系的组件 import Cmp1 from '../Child/Cmp1' import ...