博客
关于我
使用Nginx在80端口上代理多个.NET CORE网站
阅读量:459 次
发布时间:2019-03-06

本文共 4413 字,大约阅读时间需要 14 分钟。

Nginx配置与.NET Core 3.1网站部署在CentOS7上的解决方案

在CentOS7上部署两个.NET Core 3.1网站,分别监听内网IP192.168.2.32下的不同路径,实现独立访问的需求。通过Nginx配置,可以实现以下场景:访问http://192.168.2.32直接跳转至A网站,访问http://192.168.2.32/bmd/则跳转至B网站。以下是具体实现方法。

问题描述

在部署两个.NET Core 3.1网站时,若将两个网站部署在同一IP地址下的不同路径下,可能会导致以下问题:

  • 当直接访问http://192.168.2.32时,希望直接访问A网站
  • 当访问http://192.168.2.32/bmd/时,希望访问B网站
  • B网站的资源加载(如css和js)依赖于特定的路径结构
  • 解决方案

    为了解决上述问题,可以通过Nginx配置实现路径重写和资源加载的正确映射。以下是具体的配置方法。

    Nginx配置详细说明

  • 配置文件结构

    user root;worker_processes auto;error_log /var/log/nginx/error.log;pid /run/nginx.pid;# 动态模块加载include /usr/share/nginx/modules/*.conf;events { worker_connections 1024; }http {    log_format main '$remote_addr - $remote_user [$time_local] "$request" "$status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';    access_log /var/log/nginx/access.log main;    gzip on;    sendfile on;    tcp_nopush on;    tcp_nodelay on;    keepalive_timeout 65;    types_hash_max_size 2048;    include /etc/nginx/mime.types;    default_type application/octet-stream;    include /etc/nginx/conf.d/*.conf;    server {        listen 80;        listen [::]:80;        server_name web;        # 包含默认服务器配置        include /etc/nginx/default.d/*.conf;        location / {            proxy_http_version 1.1;            proxy_set_header Upgrade $http_upgrade;            proxy_set_header Connection keep-alive;            proxy_set_header Host $host;            proxy_set_header X-Real-IP $remote_addr;            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;            proxy_cache_bypass $http_upgrade;            proxy_set_header X-NginX-Proxy true;            # 处理http_referer,解决B网站资源加载问题            if ($http_referer ~ 'bmd') {                rewrite ^/(.*)$ http://$host/bmd/$1 permanent;            }            proxy_pass http://127.0.0.1:5000/;        }        location ^~/bmd/ {            root /usr/local/whitelist;            proxy_http_version 1.1;            proxy_set_header Upgrade $http_upgrade;            proxy_set_header Connection keep-alive;            proxy_set_header Host $host;            proxy_set_header X-Real-IP $remote_addr;            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;            proxy_cache_bypass $http_upgrade;            proxy_set_header X-NginX-Proxy true;            # 优化资源路径            # rewrite ^/bmd/(.*)$ /$1 break;            # proxy_redirect ~^http://192.168.2.32/bmd/(.*)$ http://127.0.0.1:5001/$1;            proxy_pass http://127.0.0.1:5001/;        }        location /Nginxstatus {            stub_status on;            access_log /usr/local/nginx/logs/status.log;            auth_basic "NginxStatus";        }        error_page 404 /404.html;        location = /40x.html { }        error_page 500 502 503 504 /50x.html;        location = /50x.html { }    }    server {        listen 8000;        listen [::]:8000;        server_name api;        # 包含默认服务器配置        include /etc/nginx/default.d/*.conf;        location /api/v1 {            proxy_pass http://127.0.0.1:5003;            proxy_http_version 1.1;            proxy_set_header Upgrade $http_upgrade;            proxy_set_header Connection keep-alive;            proxy_set_header Host $host;            proxy_set_header X-Real-IP $remote_addr;            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;            proxy_cache_bypass $http_upgrade;        }        error_page 404 /404.html;        location = /40x.html { }        error_page 500 502 503 504 /50x.html;        location = /50x.html { }    }}
  • 配置解析

    • location / { }:表示所有请求的默认处理位置
    • proxy_pass http://127.0.0.1:5000/;:将请求代理转发至backend服务器(如.NET Core 3.1应用程序)
    • if ($http_referer ~ 'bmd') { rewrite ^/(.*)$ http://$host/bmd/$1 permanent; }:根据http_referer字段判断是否需要跳转至B网站
    • location ^~/bmd/ { }:定义了 /bmd/ 路径的处理规则
    • root /usr/local/whitelist;:指定B网站的物理路径
    • proxy_pass http://127.0.0.1:5001/;:将请求代理转发至B网站的backend服务器
  • 关键配置要点

    • 使用rewrite指令实现路径重写
    • 通过proxy_set_header设置必要的HTTP头信息
    • 使用proxy_cache控制缓存策略
    • 配合proxy_pass实现请求转发
  • 实施步骤

  • 安装Nginx

    • 在CentOS7上安装Nginx:
    yum install nginx
  • 配置Nginx

    • 修改默认配置文件:
    nano /etc/nginx/conf.d/default.conf
    • 填写自定义配置
    • 保存并退出
  • 启动Nginx

    systemctl start nginxsystemctl enable nginx
  • 验证配置

    • 使用curl命令测试:
    curl -I http://192.168.2.32curl -I http://192.168.2.32/bmd/
  • 注意事项

  • 路径配置

    • 确保A网站和B网站的实际路径配置正确
    • /usr/local/whitelist需要挂载或创建为B网站的物理路径
  • 缓存设置

    • 根据实际需求调整proxy_cache_bypassproxy_cache的配置
  • 性能优化

    • 根据实际负载调整keepalive_timeoutworker_connections的值
  • 通过上述配置,可以有效实现同一IP地址下多个.NET Core 3.1网站的独立访问,同时保证资源加载的正确性。

    转载地址:http://ruubz.baihongyu.com/

    你可能感兴趣的文章
    Nessus漏洞扫描教程之配置Nessus
    查看>>
    Nest.js 6.0.0 正式版发布,基于 TypeScript 的 Node.js 框架
    查看>>
    Netpas:不一样的SD-WAN+ 保障网络通讯品质
    查看>>
    Netty WebSocket客户端
    查看>>
    Netty工作笔记0011---Channel应用案例2
    查看>>
    Netty工作笔记0014---Buffer类型化和只读
    查看>>
    Netty工作笔记0050---Netty核心模块1
    查看>>
    Netty工作笔记0084---通过自定义协议解决粘包拆包问题2
    查看>>
    Netty常见组件二
    查看>>
    netty底层源码探究:启动流程;EventLoop中的selector、线程、任务队列;监听处理accept、read事件流程;
    查看>>
    Netty核心模块组件
    查看>>
    Netty框架的服务端开发中创建EventLoopGroup对象时线程数量源码解析
    查看>>
    Netty源码—2.Reactor线程模型一
    查看>>
    Netty源码—4.客户端接入流程一
    查看>>
    Netty源码—4.客户端接入流程二
    查看>>
    Netty源码—5.Pipeline和Handler一
    查看>>
    Netty源码—6.ByteBuf原理二
    查看>>
    Netty源码—7.ByteBuf原理三
    查看>>
    Netty源码—7.ByteBuf原理四
    查看>>
    Netty源码—8.编解码原理二
    查看>>