我正在将我的 wordpress 博客从 Apache 移至 Nginx。我尝试了多个教程来让永久链接正常工作,但对我而言没有任何效果。我的网站结构如下:
main site -> www.localhost.com
wordpress blog -> www.localhost.com/blog
网站位于/var/www/html
并且 wordpress 安装在/var/www/html/blog
我读了多篇文章,看了多个视频,但似乎没有什么效果。请告诉我我哪里做错了。
我定义了两个服务器块,/etc/nginx/sites-available/default
一个用于主站点,一个用于博客。
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
charset utf-8;
error_page 404 /404.php;
location /article {
rewrite ^/article.* / redirect;
}
#location / {
# try_files $uri $uri/ /loadpage.php?$args;
#}
location ~ \.html$ {
try_files $uri /courses/index.php?$args;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php7.0-fpm:
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
server {
listen 80;
listen [::]:80;
root /var/www/html/blog;
index index.php index.html index.htm;
server_name example.com www.example.com;
location /blog/ {
try_files $uri $uri/ /blog/index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
# fastcgi_pass unix:/var/run/php/php7.1-fpm.sock; #Ubuntu 17.10
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; #Ubuntu 17.04
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
答案1
通过为同一域创建多个server
块,您实际上在虚拟服务器之间创建了冲突。server
块定义 Nginx 将监听的内容。如果给定端口有多个虚拟服务器,则请求将传递到具有最佳匹配的虚拟服务器server_name
。通过拥有多个相同的虚拟服务器,Nginx 将无法告诉将其传递到哪里。
您的主站点和 WordPress 位于同一个域(和同一个端口)下,因此应该放在一个虚拟服务器中。您可以使用location
块将它们分开。
小心Nginx 块选择算法,正则表达式块总是在正常块之前进行评估。在您的示例中,以 结尾的所有内容.html
都会发送到您的主站点。当 WordPress 尝试使用以 结尾的 URI 时,这可能会干扰它.html
。为了防止这种情况,您可以使用嵌套位置块。通过将正则表达式位置块移到 中,/
只有当父位置匹配时,才会对其进行评估。当请求进入 时/blog
,/blog
位置块将是最佳匹配块,因此将优先选择/
。由于正则表达式块现在嵌套在其中,因此不会对 WordPress 的请求进行评估。
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
charset utf-8;
error_page 404 /404.php;
location /article {
rewrite ^/article.* / redirect;
}
location / {
location ~ \.html$ {
try_files $uri /courses/index.php?$args;
}
try_files $uri $uri/ /loadpage.php?$args;
}
location /blog/ {
try_files $uri $uri/ /blog/index.php?$args;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php7.0-fpm:
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
答案2
我知道这是一篇旧帖子,但我遇到了同样的问题,却没有在任何地方找到正确的答案。这不是 .htaccess 文件或文件夹结构权限的问题。问题是将 wordpress 网站移出默认文档根文件夹。
如果您的文档根文件夹的子文件夹中有多个 wordpress 站点或单个站点,则您必须在 apache2.conf 或 httpd.conf 文件中为每个子文件夹“放宽对内容的访问”。
添加此功能后,您将能够使用“漂亮的永久链接”而不是几乎漂亮或普通的永久链接。
/etc/apache2/apache2.conf
/etc/httpd/conf/httpd.conf
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
# Added
<Directory /var/www/html/site1/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/html/site2/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
# End added