假设我的主要域名是 example.com。我在域名上安装了 SSL,并且可以成功访问域名。我首选的版本是使用 www,因此所有请求都将重定向到https://www.example.com
。
这是我迄今为止所做的,并且在访问根域时运行良好。
# redirect HTTP to HTTPS
server {
listen 80;
server_name example.com www.example.com;
rewrite ^ https://$server_name$request_uri? permanent;
}
# SSL conf
server {
root /var/www/example.com;
index index.html index.htm index.php;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
listen 443 ssl spdy;
server_name example.com www.example.com;
ssl_certificate /etc/ssl/example.com/certificate/join-cert.crt;
ssl_certificate_key /etc/ssl/example.com/server-key/ssl.key;
# Redirect non-www to www
if ($host = 'example.com' ) {
rewrite ^/(.*)$ https://www.example.com/$1 permanent;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
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 ~ /\.ht {
deny all;
}
}
问题是我在不同的目录中安装了 wordpress。比如说https://www.example.com/blog/
。但我无法访问任何文章(https://www.example.com/blog/artcle1/
)。我使用 CloudFlare,它显示“找不到您要查找的页面”。
请建议我正确的虚拟主机。
注意:我知道在根目录上安装 wordpress 并将主页从 wordpress 设置更改为 很容易https://www.example.com/blog/
。我不想这样做,因为我的根域上有 flash。我会将 wordpress 保留在该目录中。
答案1
首先,你使用过于复杂的方式进行非 www 重定向www
。你应该这样做:
server {
listen 443 ssl spdy;
server_name example.com;
ssl_certificate /etc/ssl/example.com/certificate/join-cert.crt;
ssl_certificate_key /etc/ssl/example.com/server-key/ssl.key;
return 301 https://www.example.com$request_uri;
}
然后,您遇到的实际问题,此配置应该可以解决它:
server {
root /var/www/example.com;
index index.html index.htm index.php;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
listen 443 ssl spdy;
server_name www.example.com;
ssl_certificate /etc/ssl/example.com/certificate/join-cert.crt;
ssl_certificate_key /etc/ssl/example.com/server-key/ssl.key;
location /blog {
try_files $uri $uri/ /blog/index.php?$args;
}
location ~ \.php$ {
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 ~ /\.ht {
deny all;
}
}
因此,我们在这里将重定向更改为在/blog
URI 上工作。此外,您需要将 Wordpress 根 URL 设置更改为https://www.example.com/blog
,否则 Wordpress 无法正确识别 WP 安装内的位置。