首页 > 解决方案 > NginX Ubuntu 20.04 PHP 7.4 上的 wordpress /blog 文件出现 404 错误

问题描述

我在我们网站的根目录中运行一个 VueJS 系统,驱动同一服务器上的 Django API。

example.com 是 beta 测试版。

我想将 wordpress 加载到子文件夹example.com/blog

我正在运行 Nginx [没有 Apache - 并且未安装 Apache]。

已安装 PHP 和 MySQL 并编辑我们的服务器块。请参阅下面的代码。

我尝试在 /blog 中访问的任何文件都出现 404 错误,例如

https://example.com/blog/readme.html 
https://example.com/blog/test.php 
https://example.com/blog/index.php 
https://example.com/blog/wp-admin/install.php

这是我们的服务器块代码 {IP masked out}

server {
    server_name example.com www.example.com 128.199.###.###;

    root /var/www/html/vue/example/dist;

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

    location /static/ {
        alias /home/example/src;
    }

    location / {
        try_files $uri $uri/ /index.html /index.php;
    }

    location ^~ /rest-auth/ {
    include proxy_params;
    proxy_pass http://unix:/run/gunicorn.sock;
    }

    location ^~ /api/ {
    include proxy_params;
    proxy_pass http://unix:/run/gunicorn.sock;
    }

    location ~ /\.ht {
        deny all;
    }

    location ^~ /admin {
    include proxy_params;
    proxy_pass http://unix:/run/gunicorn.sock;
    }

    location ^~ /blog {
      alias /var/www/html;
      index index.php index.html index.htm;
      try_files $uri =404;
    }

# For security reasons, set php settings at root level.
# This prevents root level php files from showing as plain text.
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }

    error_log  /var/log/nginx/example-error.log;
    access_log /var/log/nginx/example-access.log;

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}
server {
    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name example.com www.example.com 128.199.###.###;
    return 404; # managed by Certbot

}


非常感谢您的帮助-我查看了很多资源并尝试了很多东西但没有成功-谢谢。

标签: phpnginxubuntu-20.04

解决方案


chrome 下载问题掩盖了代码中一个简单的错误路径!Firefox 显示了一个简单的 404,它很容易调试!

最终的解决方案是为博客添加这个位置:

location /blog {
          alias /var/www/html/blog;
          try_files $uri $uri/ @blog;

           location ~ \.php$ {
               include snippets/fastcgi-php.conf;
               fastcgi_param SCRIPT_FILENAME $request_filename;
               fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
            }
         }

     location @blog {
         rewrite /blog/(.*)$ /blog/index.php?/$1 last;
     }

推荐阅读