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

Nginx多站点配置问题:如何通过子路径访问不同根目录站点

配置方案

要实现http://example.com/cert-portal对应/var/www/cert-portal/public、http://example.com/wiki对应/var/www/mediawiki,你需要将两个独立的server块合并为一个,通过location块分别映射子路径,具体配置如下:

server {
    listen 80;
    listen [::]:80;
    server_name example.com;

    # 证书门户站点配置:/cert-portal 映射到 /var/www/cert-portal/public
    location /cert-portal {
        alias /var/www/cert-portal/public;
        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-Content-Type-Options "nosniff";
        charset utf-8;
        index index.php;

        try_files $uri $uri/ /cert-portal/index.php?$query_string;

        location = /cert-portal/favicon.ico { access_log off; log_not_found off; }
        location = /cert-portal/robots.txt  { access_log off; log_not_found off; }

        error_page 404 /cert-portal/index.php;

        location ~ /cert-portal/.+\.php$ {
            fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            include fastcgi_params;
        }

        location ~ /cert-portal/\.(?!well-known).* {
            deny all;
        }
    }

    # MediaWiki站点配置:/wiki 映射到 /var/www/mediawiki
    location /wiki {
        alias /var/www/mediawiki;
        index index.php;
        client_max_body_size 5m;
        client_body_timeout 60;

        try_files $uri $uri/ @wiki-rewrite;

        location ^~ /wiki/maintenance/ {
            return 403;
        }

        location /wiki/rest.php {
            try_files $uri $uri/ /wiki/rest.php?$args;
        }

        location ~ /wiki/.+\.php$ {
            include fastcgi_params;
            fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $request_filename;
        }

        location ~* /wiki/.*\.(js|css|png|jpg|jpeg|gif|ico)$ {
            try_files $uri /wiki/index.php;
            expires max;
            log_not_found off;
        }

        location = /wiki/_.gif {
            expires max;
            empty_gif;
        }

        location ^~ /wiki/cache/ {
            deny all;
        }

        location /wiki/dumps {
            alias /var/www/mediawiki/local/dumps;
            autoindex on;
        }
    }

    # 全局默认规则(可选)
    location / {
        return 404;
    }
}

# 定义wiki的重写规则
location @wiki-rewrite {
    rewrite ^/wiki/(.*)$ /wiki/index.php?title=$1&$args;
}

关键修改说明

  • 合并server块:将原来两个独立的server合并为一个,server_name设置为example.com,所有请求都由这个块处理。
  • 使用alias替代root:因为子路径对应的实际目录不是根目录的子文件夹,alias能更准确地将请求路径映射到文件系统路径。
  • 添加子路径前缀:所有原配置中的根路径规则(比如/favicon.ico、/rest.php)都需加上子路径前缀(/cert-portal/xxx、/wiki/xxx),确保请求匹配到对应站点的规则。
  • 修正PHP路径:PHP处理的SCRIPT_FILENAME使用$request_filename,结合alias自动生成正确的文件路径,避免手动拼接错误。
  • 调整重写规则:MediaWiki的重写规则需添加/wiki前缀,确保重写后的请求指向正确的站点入口。

配置完成后,重启Nginx生效:

sudo systemctl restart nginx

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 01:20:33