首页 > 解决方案 > 配置 nginx 从子目录运行 wordpress 但 url 中没有子目录名称的问题

问题描述

我对 nginx 很陌生。我有两种类型的代码,一种是简单的 php 运行,index2.php我有一个在其中命名的目录wordpress,它有一个完整的 wordpress 网站。

我想要实现的是让它们都在主域上运行,而没有带有子目录名称的斜线。

location ~ (/lottery-results|patternTwo) {
    try_files $uri /index2.php$is_args$args;
}
location / {
    try_files $uri /wordpress/index.php?$args;
}

这是我目前使用的配置,它可以很好地满足我的目的。第一个指令通过简单的 php in 加载一些 url index2.php。第二个指令加载 wordpress,但是当我打开这个 url 时:

http://domain.test/

它把我送到:

http://domain.test/wordpress/wp-admin/setup-config.php

我的问题是/wordpress我希望网址为:

http://domain.test/wp-admin/setup-config.php

反而。

我尝试使用 alias 和 root 但它没有改变任何东西,(老实说,我什至不明白 alias 和 root 的作用!)

编辑:PHP-FPM 处理程序:

        location ~ \.php$ {
            try_files        $uri =404;
            fastcgi_pass     unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI_phpMAMP_PhpLocalhost_MAMP.sock;
            fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include          /Applications/MAMP/conf/nginx/fastcgi_params;
        }

标签: nginxconfignginx-config

解决方案


让我们试试这个:

# This block should be OUTSIDE the server block!
map $request_uri $new_uri {
    ~^/wordpress(/.*)  $1;
    default            $request_uri;
}

server {
    ...
    location ~ (/lottery-results|patternTwo) {
        try_files $uri /index2.php$is_args$args;
    }
    location / {
        try_files $uri /wordpress$uri /wordpress/index.php?$args;
    }

    location ~ \.php$ {
        try_files $uri =404;

        # Here the order of directives DOES matter
        # This should be the first:
        include /Applications/MAMP/conf/nginx/fastcgi_params;
        # This should be the second:
        fastcgi_param REQUEST_URI $new_uri;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI_phpMAMP_PhpLocalhost_MAMP.sock;
    }
}

推荐阅读