Nginx Wordpress 子目錄 + wp admin

Nginx Wordpress 子目錄 + wp admin

我知道還有其他類似的線程,但我已經嘗試了好幾天,目前無法透過 Ubuntu 18 上的以下 nginx 配置

server {
        listen 80;
        root /var/www/html/wordpress/public_html;
        index index.php index.html;
        server_name "example.com";

    access_log /var/log/nginx/SUBDOMAIN.access.log;
        error_log /var/log/nginx/SUBDOMAIN.error.log;

 location /blog {
         index index.php index.html index.htm;
         try_files $uri $uri/ /blog/index.php?q=$uri&$args;

        location ~ \.php$ {
                     include snippets/fastcgi-php.conf;

                     fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }
    }

        location ~ /\.ht {
                     deny all;
        }

        location = /favicon.ico {
                     log_not_found off;
                     access_log off;
        }

        location = /robots.txt {
                     allow all;
                     log_not_found off;
                     access_log off;
       }

        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                     expires max;
                     log_not_found off;
       }}

這是我的資料夾目前結構

  • /var/www/html/wordpress/public_html -> 包含資料夾 blog 和一個簡單的 index.html (不提供 WordPress)
  • /var/www/html/wordpress/public_html/blog -> 是我的 WordPress 檔案位置

但是,有兩個錯誤的部分我需要解決。

  1. 它顯示“mysite.com/blog”,但是當我單擊任何部落格或網址時-> 它顯示“example.com/?p=1”,因此導致簡單的index.html位於位置/(“example.com ” ”)
  2. 我也無法訪問 wp-admin,如果我輸入“example.com/blog/wp-admin”->“http://example.com/wp-login.php?redirect_to=http%,它將重定向到此3A%2F%2Fexample.com%2Fblog%2Fwp-admin%2F&reauth=1 -> 然後404

太感謝了。

更新2: 我嘗試將 WP_SITEURL 更改為 example.com 並將 WP_HOME 更改為 example.com/blog 也修改了 mysql wp-options 表中的這兩個記錄。但是,當我嘗試訪問 /blog/wp-admin ->http://example.com/blog/wp-admin/example.com/blog/wp-login.php?redirect_to=http%3A%2F%2Fexample.com%2Fblog%2Fwp-admin%2F&reauth=1-> 404

答案1

肯定你需要

root /var/www/html/wordpress/public_html/blog;

在街區裡location /blog {}

此外,該區塊location ~ \.php$應如下所示:

location ~ \.php$ {
    try_files $uri =404;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}

另外,您需要將 try_files 指令變更為 try_files $uri $uri/ /blog/index.php?$args;

相關內容