【问题标题】:Problem configuring nginx to run wordpress from subdirectory but without subdirectory name in url配置 nginx 从子目录运行 wordpress 但 url 中没有子目录名称的问题
【发布时间】:2021-06-24 00:46:14
【问题描述】:

我对 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;
}

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

http://domain.test/

它把我送到:

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

我的问题是 /wordpress 部分我希望 url 是:

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;
        }

【问题讨论】:

  • 你正在尝试做一些非常奇怪的事情。为什么不能将index2.php 和 WordPress 文件放在同一个目录中?那些lottery-results|patternTwo 字符串是什么?为什么您不能在某些 URI 前缀下使用您的第一个站点,例如lottery-results?好吧,我认为有办法让它工作,你需要改变REQUEST_URI PHP-FPM 参数。您可以将您的 nginx PHP-FPM 处理程序(location 块)添加到您的问题中吗?
  • 您好,感谢您的评论,不仅仅是 index2.php,index2.php 在其中运行了许多其他 php 文件,就像 WordPress 通过 index.php 一样,这些字符串是 index2 的其他路径.php 支持,我不能将我的第一个网站放在 uri 前缀下,因为它有一个 api,并且使用该 api 运行的应用程序都会失败。我不知道你所说的 php-fpm 处理程序位置块是什么意思。
  • 通常 PHP-FPM 处理程序块看起来像 location ~ \.php$ { ... fastcgi_pass <socket>; ... } 或类似的东西。
  • @IvanShatsky 我想我找到了我不确定虽然我使用的是 mamp pro,但我在主 nginx.conf 中找到了这个,因为各个网站不会在你的 mamp 中创建不同的 conf可以只使用 ui 编辑器。

标签: nginx config nginx-config


【解决方案1】:

让我们试试这个:

# 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;
    }
}

【讨论】:

  • 非常感谢您介意再解释一下吗?
  • 当然可以,但稍等片刻,我将有几个空闲时间。此配置是否按预期工作?同时您可以查看this 问题和我回答的第一部分。
  • 只要有时间就不要着急,那就太好了!当然它工作得很好。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-06
  • 1970-01-01
  • 1970-01-01
  • 2015-04-25
  • 2016-11-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多