在子文件夹中为 nginx 网站设置正确的根/重定向路径

Setting the proper root / redirect path for nginx website in subfolder

我有 2 个可以同时访问的网站 URL,只有一个可以从这样的子文件夹访问:

website.com/

网站。com/blog

现在他们在 Apache 下运行,我想切换到 Nginx,我偶然发现了 Nginx 在这种方法中的重写规则。

托管服务器是带有 Plesk 的 Ubuntu,因此我唯一需要编辑的部分是负责根据 URL 格式重写的部分。

原起点是这个:

if (!-e $request_filename) {
    set $test P;
}
if ($uri !~ ^/(plesk-stat|webstat|webstat-ssl|ftpstat|anon_ftpstat|awstats-icon|internal-nginx-static-location)) {
    set $test "${test}C";
}
if ($test = PC ) {
    rewrite ^/(.*)$ /index.php?;
}

这对主网站非常有效,但是,位于 blog 的网站将显示主页,但任何其他内页将 return 出现 404 错误。

所以我的第一个方法是这个:

if (!-e $request_filename) {
    set $test P;
}
if ($uri !~ ^/(plesk-stat|webstat|webstat-ssl|ftpstat|anon_ftpstat|awstats-icon|internal-nginx-static-location)) {
    set $test "${test}C";
}
if ($uri ~ ^/(*blog*)) {
    set $test "B";
}
if ($test = PC ) {
    rewrite ^/(.*)$ /index.php?;
}
if ($test = B ) {
    rewrite ^/(.*)$ /blog/index.php?;
}

但这行不通,所以我尝试了一种基于位置的方法,如下所示:

location  / {

if (!-e $request_filename) {
    set $test P;
}
if ($uri !~ ^/(plesk-stat|webstat|webstat-ssl|ftpstat|anon_ftpstat|awstats-icon|internal-nginx-static-location)) {
    set $test "${test}C";
}
if ($test = PC ) {
    rewrite ^/(.*)$ /index.php?;
}

}


location ~ /blog {

root httpdocs/blog

if (!-e $request_filename) {
    set $test P;
}
if ($uri !~ ^/(plesk-stat|webstat|webstat-ssl|ftpstat|anon_ftpstat|awstats-icon|internal-nginx-static-location)) {
    set $test "${test}C";
}
if ($test = PC ) {
    rewrite ^/(.*)$ /index.php?;
}

}

它适用于当前配置:

if (!-e $request_filename) {
    set $test P;
}
if ($uri !~ ^/(plesk-stat|webstat|webstat-ssl|ftpstat|anon_ftpstat|awstats-icon|internal-nginx-static-location|blog)) {
    set $test "${test}C";
}
if ($uri ~ ^/(blog)) {
    set $test "${test}B";
}
if ($test = PC ) {
    rewrite ^/(.*)$ /index.php?;
}
if ($test = PB ) {
    rewrite ^/(.*)$ /blog/index.php?;
}