我最近将我们的主页迁移到 ASP.NET Core 1.0。这使我可以在 linux 环境中移动网站。我们在这个网站下也有 /blog,这是一个 wordpress 博客。除 W3 Total Cache 外,所有迁移均正确。这是我所做的。
安装的 PHP-FPM 和 DNX 都位于 Nginx 上的反向代理之后。这是文件夹层次结构。/var/www/aspnet /var/www/wordpress
这里是所有 Nginx 相关的配置文件
/etc/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
log_format scripts '$document_root$fastcgi_script_name > $request';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Run Code Online (Sandbox Code Playgroud)
/etc/nginx/sites-available/xyz.com
server {
listen 443 ssl http2;
server_name xyz.com www.xyz.com;
ssl_certificate /etc/ssl/certs/cert_chain.crt;
ssl_certificate_key /etc/ssl/private/xyz.private.txt;
access_log /var/log/nginx/scripts.log scripts;
# Global restrictions configuration file.
# Designed to be included in any server {} block.
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~ /\. {
deny all;
}
# Deny access to any files with a .php extension in the uploads directory
# Works in sub-directory installs and also in multisite network
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~* /blog/(?:uploads|files)/.*\.php$ {
deny all;
}
gzip_types text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
location ~ ^/blog/\.(css|htc|less|js|js2|js3|js4)$ {
expires 31536000s;
add_header Pragma "public";
add_header Cache-Control "max-age=31536000, public";
}
location ~ \.(html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml)$ {
expires 3600s;
add_header Pragma "public";
add_header Cache-Control "max-age=3600, public";
}
location ~ \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|json|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|wav|wma|wri|woff|xla|xls|xlsx|xlt|xlw|zip)$ {
expires 31536000s;
add_header Pragma "public";
add_header Cache-Control "max-age=31536000, public";
}
location / {
proxy_pass http://unix:/var/www/aspnet/kestrel.sock;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache_bypass $http_upgrade;
}
location /phpmyadmin/ {
alias /var/www/phpMyAdmin/;
index index.php;
}
location ~ ^/phpmyadmin/(.+\.php)$ {
alias /var/www/phpMyAdmin/$1;
fastcgi_pass unix:/run/php/phpmyadmin.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
# From fastcgi_params
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT /var/www/phpMyAdmin;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param REDIRECT_STATUS 200;
}
location /blog/ {
try_files $uri $uri/ index.php?q=$request_uri;
alias /var/www/wordpress/;
index index.php;
}
location ~ \.php$ {
include /var/www/wordpress/nginx.conf;
try_files $uri $uri/ index.php?q=$request_uri =404;
alias /var/www/wordpress/$1;
fastcgi_pass unix:/run/php/phpmyadmin.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# From fastcgi_params
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT /var/www/wordpress;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param REDIRECT_STATUS 200;
}
}
Run Code Online (Sandbox Code Playgroud)
/var/www/wordpress/nginx.conf - 这是 W3 Total Cache 插件生成的文件。
# BEGIN W3TC Page Cache core
set $w3tc_rewrite 1;
if ($request_method = POST) {
set $w3tc_rewrite 0;
}
if ($query_string != "") {
set $w3tc_rewrite 0;
}
if ($request_uri !~ \/$) {
set $w3tc_rewrite 0;
}
if ($http_cookie ~* "(comment_author|wp\-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle)") {
set $w3tc_rewrite 0;
}
if ($http_cookie ~* "(w3tc_preview)") {
set $w3tc_rewrite _preview;
}
set $w3tc_ssl "";
if ($scheme = https) {
set $w3tc_ssl _ssl;
}
set $w3tc_enc "";
if ($http_accept_encoding ~ gzip) {
set $w3tc_enc _gzip;
}
set $w3tc_ext "";
if (-f "$document_root/var/www/wordpress/wp-content/cache/page_enhanced/$http_host/$request_uri/_index$w3tc_ssl$w3tc_rewrite.html$w3tc_enc") {
set $w3tc_ext .html;
}
if (-f "$document_root/var/www/wordpress/wp-content/cache/page_enhanced/$http_host/$request_uri/_index$w3tc_ssl$w3tc_rewrite.xml$w3tc_enc") {
set $w3tc_ext .xml;
}
if ($w3tc_ext = "") {
set $w3tc_rewrite 0;
}
if ($w3tc_rewrite = 1) {
rewrite .* "/var/www/wordpress/wp-content/cache/page_enhanced/$http_host/$request_uri/_index$w3tc_ssl$w3tc_rewrite$w3tc_ext$w3tc_enc" last;
}
# END W3TC Page Cache core
Run Code Online (Sandbox Code Playgroud)
一旦我完成了上面的配置。我能够运行 wordpress 的永久链接。但是,当我打开 /blog/ 或 /blog/wp-admin/ 时,它显示未找到。出于故障排除目的,我在 nginx 中添加了一些自定义日志记录,如下所示。
log_format scripts '$document_root$fastcgi_script_name > $request';
Run Code Online (Sandbox Code Playgroud)
这是日志显示的内容
/var/www/wordpress//blog/wp-admin/index.php > GET /blog/wp-admin/index.php HTTP/2.0 /var/www/wordpress//blog/index.php > GET /blog/ HTTP/2.0
我尝试了很多解决方案。他们都假设父站点是 wordpress。在我的情况下,父站点建立在 DotNet Core 1 上。/blog 是我的 wordpress 博客。该问题必须是错误的重写规则之一。
总结一下这个问题,I W3 Total Cache 不是问题。没有那个我也能活。手头的问题是关于将 wordpress 站点作为静态站点的子域托管时的重写规则。可以忽略 W3 总缓存配置部分。我有像 mydomain.com/blog 这样的托管网站,但没有应用重写规则。到目前为止,我尝试了很多替代方案。如果有人将 Wordpress 实现为纯子目录而不是多站点子目录。他们可以提供他们成功的配置。
这并不能直接回答您的问题,但可能是您可能没有考虑过的更好选择。不使用插件进行缓存,而是使用 Nginx 页面缓存。它要快得多,因为您不必调用 PHP,这消除了很多开销。
缺点是使 Nginx 缓存失效非常棘手,除非您付费购买商业版本的 Nginx。您可以使用完成这项工作的插件来构建 Nginx,但是 Wordpress / Nginx 缓存集成并不是很好。我发现没有一个工作得很好。因此,您需要仔细设置缓存最长寿命。有趣的是,在繁忙的站点上,即使缓存几秒钟也会有好处。我的网站很少更改,如果需要,我可以直接 rm -rf nginx 页面缓存所在的正确目录 - 实际上位于内存中。
我在这里有一个关于此的教程,周围还会有很多其他教程。这里有一篇关于Nginx 微缓存的精彩文章。
SF 喜欢页面上的实际数据,以防网站消失。
在你的 nginx.conf 中
fastcgi_cache_key "$scheme$request_method$host$request_uri";
Run Code Online (Sandbox Code Playgroud)
在站点文件的顶部,或者在 nginx 配置中
fastcgi_cache_path /dev/shm/nginxcache levels=1:2 keys_zone=CACHENAME:10m inactive=1440m; # Centos / Amazon Linux in RAM, 1440 minutes = 24 hours
Run Code Online (Sandbox Code Playgroud)
在调用 PHP 的位置块中
fastcgi_pass php56-fpm;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
fastcgi_cache CACHENAME;
fastcgi_cache_valid 200 1440m;
fastcgi_cache_valid 403 404 405 410 414 301 302 307 60m;
add_header X-Cache $upstream_cache_status; # This can be removed if desired
fastcgi_cache_methods GET HEAD;
fastcgi_keep_conn on;
Run Code Online (Sandbox Code Playgroud)
我链接到的教程有更多信息和解释。