我将一个子目录/博客添加到我的服务器上,这是一个WordPress的安装。
我已经在nginx上设置了以下configuration,但在/博客上发生了404错误。 网站/configuration的其余部分是完美的。
我错过了什么?
domain.com
server { listen 80; listen [::]:80; server_name domain.com; return 301 https://domain.com$request_uri; } server { listen 443 ssl; server_name domain.com; ssl_certificate /etc/ssl/domain_com-bundle.crt; ssl_certificate_key /etc/ssl/domain_com.key; root /opt/domain.com/public/; location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location /blog { root /var/www/html/siteWordpress/; index index.php index.html index.htm; try_files $uri $uri/ /index.php?q=$uri&$args; } location / { try_files $uri @backend; } location @backend { proxy_pass http://127.0.0.1:3030; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } }
WordPress使用PHP,目前您已经为文档根目录/opt/domain.com/public/设置了PHP,但是不能用于文件根目录/var/www/html/siteWordpress/ 。
你需要添加一个嵌套的位置块:
location ^~ /blog { root /var/www/html/siteWordpress/; index index.php index.html index.htm; try_files $uri $uri/ /blog/index.php?q=$uri&$args; location ~ \.php$ { ... } }
^~修饰符是使前缀位置优先于同级别的其他正则expression式位置所必需的。 请参阅此文档了解更多
try_files语句的默认URI应该以/blog为前缀,否则将会执行错误的index.php 。
上面假设WordPress安装在/var/www/html/siteWordpress/blog/ ,如果情况不是这样,你的configuration就变得复杂得多了。
WordPress需要知道它被托pipe在一个子目录中,例如,通过向wp-config.php添加以下内容:
define('WP_HOME','http://example.com/blog'); define('WP_SITEURL','http://example.com/blog');