使用 Nginx 实现按路径分流

发布于 2023-11-25  401 次阅读


AI 摘要

使用Nginx实现按路径分流的方法很简单。首先,在服务器上部署Nginx,可以选择使用Docker来省去一些配置的麻烦。在Docker的compose文件中,设置Nginx的网络模式为host,并挂载必要的配置文件、日志文件和网页文件。 在Nginx的配置文件中,我们可以根据不同的路径来进行分流。首先设置根路径的配置,指定网页文件的位置和默认文件。然后,我们可以使用`location`指令来分别配置不同路径的转发。通过设置`proxy_pass`指令,我们可以将不同的路径转发到不同的本地端口上。注意,`proxy_pass`指令的末尾需要加上斜杠。 例如,通过配置如下的Nginx文件,访问`http://127.0.0.1/demo/`将会被转发到`http://127.0.0.1:9600`,而访问`http://127.0.0.1/demo2/`将会被转发到`http://127.0.0.1:9500`。 此外,如果你想将路径`http://127.0.0.1/demo/test/`分发到`http://127.0.0.1:9600/test/`,可以使用`rewrite`指令进行重写,并将请求转发到相应的地址。 如果需要对外开放IP访问,你可以在`server_name`中添加本机IP,并根据需要在防火墙中开启相应端口。在CentOS 7中,默认安装了firewalld防火墙,你可以使用相应的命令来开启、关闭、禁用以及查询端口情况。 总之,使用Nginx可以很方便地实现按路径分流的需求,而且配置也比较简单。

场景

在只使用一个域名的情况下,有时候希望按不同的路径,将请求分发到不同本地端口的服务。显然可以使用 Nginx 实现。

方法

先部署 Nginx,这里选择 docker,省的折腾 CentOS7……网络模式选 host,省的配端口。

docker-compose 配置

version: '3'
services:
    nginx:
        image: nginx:latest
        container_name: nginx
        restart: unless-stopped
        network_mode: host
        volumes:
            - ./data/conf.d:/etc/nginx/conf.d
            - ./data/nginx.conf:/etc/nginx/nginx.conf
            - ./data/logs:/var/log/nginx
            - ./data/html:/var/www/html

注意,这里的 nginx.conf 需要你事先准备好。可以是网上找一个默认配置,或者先 docker 启动一个不带多余参数的容器,将生成的文件复制过来。

Nginx 配置

conf.d 新建配置文件,参考如下:

server {
    listen 80;
    server_name  127.0.0.1;

    location / {
        root /var/www/html;
        index  index.html index.htm;
    }

    # service 1
    location /demo1/ {
        proxy_pass http://127.0.0.1:9600/;
        client_max_body_size 128m;
        proxy_connect_timeout 60;
        proxy_read_timeout 60;
        proxy_send_timeout 60;
        proxy_intercept_errors off;
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;   
    }

    # service 2
    location /demo2/ {
        proxy_pass http://127.0.0.1:9500/;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

注意,proxy_pass 最后的 / 不能省。举例:

  • 以上配置的效果是:访问 http://127.0.0.1/demo/ 等同于访问 http://127.0.0.1:9600
  • 如果去掉末尾的 /,访问 http://127.0.0.1/demo/ 等同于访问 http://127.0.0.1:9600/demo/

看你的需求了。

注意

如果你想将路径 http://127.0.0.1/demo/test/ 分发到 http://127.0.0.1:9600/test/,那么 location 应该使用 rewrite 重写,例如:

    location /demo/test/ {
        rewrite /demo/(.*) /$1 break;
        proxy_pass http://127.0.0.1:9600;
        client_max_body_size 128m;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection Upgrade;
    }

防火墙

你可以在 server_name 中添加本机 IP,再开启防火墙,以实现 IP 访问。

CentOS7 默认装了 firewalld,但好像不开 80 端口,需要手动开。

  • 启动防火墙:systemctl start firewalld
  • 关闭防火墙:systemctl stop firewalld
  • 禁用防火墙:systemctl disable firewalld
  • 重启防火墙:firewall-cmd --reload
  • 查看版本:firewall-cmd --version
  • 当前状态:firewall-cmd --state
  • 查询端口情况:firewall-cmd --query-port=80/tcp
  • 列出开放的端口:firewall-cmd --permanent --list-ports
  • 开放端口(永久):firewall-cmd --permanent --add-port=80/tcp
  • 开放端口(区间,永久):firewall-cmd --permanent --add-port=8083-8085/tcp
  • 关闭端口(区间,永久):firewall-cmd --permanent --remove-port=8083-8085/tcp

注意,如果是永久开放、移除了某个端口,需要 reload 一次才会生效。