如何在 CentOS 上使用 Nginx 设置两个具有两个不同端口的块服务器(基于 Ip)?

How to setup two block server (Ip based) with two differents ports with Nginx on CentOS?

我在 CentOS 8 我的 nginx 配置文件在 /etc/nginx/nginx.conf

我没有任何 DNS,因此我只会使用服务器 ip 来访问服务器,我在 one 中搜索主机 两个网站 的方法Nginx 服务器(没有 DNS)

感谢您的帮助:))

Ps。我认为最好的方法是使用两个不同的端口,但我不知道如何

我认为你的选择是

  • 使用两个不同的端口,例如:

    server {
        listen 80 default_server; # listening port for site #1
        ... # site #1 configuration
    }
    server {
        listen 81 default_server; # listening port for site #2
        ... # site #2 configuration
    }
    

    如果您有启用 SELinux 的系统,用于此配置的端口必须标记为 http_port_t 类型,否则 nginx 将无法绑定到这些端口。您可以使用

    检查可用的 http_port_t 端口
    semanage port -l|grep http_port_t
    

    命令。要用 http_port_t 类型标记一些新端口(例如,端口 81),请使用

    semanage port -a -t http_port_t -p tcp 81
    

    不要忘记打开用于通过防火墙进行外部访问的端口。

  • 在某些 URI 前缀下提供第二个站点,例如,对第一个站点使用 http://<ip>/,对第二个站点使用 http://<ip>/blog/

    server {
        listen 80 default_server;
        location / {
            # configuration for site #1
            root /path/to/site1;
            ...
        }
        location /blog/ {
            # configuration for site #2
            alias /path/to/site2/;
            ...
        }
    }
    

    使用此配置可​​能会很棘手,因为您网站 #2 上的每个 link 都必须是相对的或以 /blog/ 前缀开头,否则 link 将无法使用location /blog/ { ... } 块因此导致站点 #1。