You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无域名VPS下使用NGINX配置多PHP站点的问题求助

解决Nginx单IP多站点路径访问问题

问题背景

拥有一台VPS,未使用域名,服务器部署NGINX(不熟悉其配置)。需通过12.34.56.78/siteA和12.34.56.78/siteB的URL访问两个不同站点,后续将分配独立域名,但当前只能用IP访问。目前配置后无法通过路径访问站点,直接访问IP仅显示其中一个站点,且路径不符。

当前配置及错误信息

站点文件结构

$ sudo ls /etc/nginx/sites-enabled/
siteA  siteB

$ sudo cat /var/www/html/siteA/index.php
this is SITE A at /var/www/html/siteA/index.php

$ sudo cat /var/www/html/siteB/index.php
this is SITE B at /var/www/html/siteB/index.php

原SITE A配置

$ sudo cat /etc/nginx/sites-available/siteA
server {
  listen 80;
  listen [::]:80;

  server_name 12.34.56.78;      
  root /var/www/html/siteA;
  index index.php index.html index.htm;

  location /siteA {
    try_files $uri $uri/ /siteA/index.php?$args;
  }

  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;
  }

  location ~ /\.ht {
    deny all;
  }
}

原SITE B配置

与SITE A配置一致,仅替换为siteB:

$ sudo cat /etc/nginx/sites-available/siteB
server {
  listen 80;
  listen [::]:80;

  server_name 12.34.56.78;      
  root /var/www/html/siteB;
  index index.php index.html index.htm;

  location /siteB {
    try_files $uri $uri/ /siteB/index.php?$args;
  }

  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;
  }

  location ~ /\.ht {
    deny all;
  }
}

配置应用结果

$ sudo nginx -t && sudo service nginx reload && sudo service nginx restart
nginx: [warn] conflicting server name "12.34.56.78" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "12.34.56.78" on [::]:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

# 重置日志
$ echo "" | sudo tee /var/log/nginx/error.log

测试错误

$ curl http://12.34.56.78/siteA
404 Not Found

$ curl http://12.34.56.78/siteB
404 Not Found

$ curl http://12.34.56.78
this is SITE A at /var/www/html/siteA/index.php

$ tail -f /var/log/nginx/error.log
2023/12/30 22:50:17 [error] 119546#119546: *2 open() "/var/www/html/siteA/index.php" failed (2: No such file or directory), client: 12.34.56.78, server: 12.34.56.78, request: "GET /test HTTP/1.1", host: "12.34.56.78"

问题分析

  1. Server名称冲突:两个server块使用了相同的server_name(IP地址),NGINX会自动忽略第二个配置,因此只有第一个站点(siteA)生效,直接访问IP时显示siteA内容。
  2. 路径映射错误:每个server的root设置为站点目录,当请求/siteA时,NGINX会在/var/www/html/siteA下查找siteA子目录(即/var/www/html/siteA/siteA/),该目录不存在,导致404错误。

解决方案

步骤1:禁用原有冲突配置

sudo unlink /etc/nginx/sites-enabled/siteA
sudo unlink /etc/nginx/sites-enabled/siteB

步骤2:创建新的统一配置文件

新建/etc/nginx/sites-available/multisite文件,内容如下:

server {
    listen 80;
    listen [::]:80;

    server_name 12.34.56.78;

    index index.php index.html index.htm;

    # 处理siteA请求:用alias替换root,避免路径嵌套
    location /siteA {
        alias /var/www/html/siteA;
        try_files $uri $uri/ /siteA/index.php?$args;

        # 嵌套处理PHP文件,修正SCRIPT_FILENAME参数
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        }
    }

    # 处理siteB请求,逻辑与siteA一致
    location /siteB {
        alias /var/www/html/siteB;
        try_files $uri $uri/ /siteB/index.php?$args;

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        }
    }

    # 禁止访问.ht文件
    location ~ /\.ht {
        deny all;
    }

    # 可选:直接访问IP时返回403或默认页面
    location / {
        return 403 Forbidden;
        # 若需默认页面,可替换为:
        # root /var/www/html/default;
        # index index.html;
    }
}

步骤3:启用新配置并重启NGINX

sudo ln -s /etc/nginx/sites-available/multisite /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl restart nginx

步骤4:测试验证

curl http://12.34.56.78/siteA
# 应返回:this is SITE A at /var/www/html/siteA/index.php

curl http://12.34.56.78/siteB
# 应返回:this is SITE B at /var/www/html/siteB/index.php

curl http://12.34.56.78
# 应返回403 Forbidden(或自定义默认页面内容)

配置说明

  • 单Server块:避免server_name冲突,确保所有路径规则都生效。
  • Alias指令:与root不同,alias会直接将请求路径替换为指定目录,比如/siteA/index.php会映射到/var/www/html/siteA/index.php,而非/var/www/html/siteA/siteA/index.php。
  • 嵌套PHP处理:在每个站点的location内嵌套PHP规则,确保PHP文件能被正确解析,同时修正SCRIPT_FILENAME参数适配alias路径。

内容的提问来源于stack exchange,提问作者Fahed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 04:27:17