在文件系统中...

在文件系统中...

我最近将我们的主站点迁移到了 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/*;

}

/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;          
    }
}

/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

完成上述配置后,我就可以运行 wordpress 的永久链接了。但是,当我打开 /blog/ 或 /blog/wp-admin/ 时,它显示未找到。为了进行故障排除,我在 nginx 中添加了一些自定义日志记录,如下所示。

log_format scripts '$document_root$fastcgi_script_name > $request';

日志显示的内容如下

/var/www/wordpress//blog/wp-admin/index.php > 获取 /blog/wp-admin/index.php HTTP/2.0 /var/www/wordpress//blog/index.php > 获取 /blog/ HTTP/2.0

我尝试了很多解决方案。它们都假设父站点是 wordpress。在我的例子中,父站点是基于 DotNet Core 1 构建的。/blog 是我的 wordpress 博客。问题肯定是错误的重写规则之一。

总结一下,我认为 W3 Total Cache 不是问题。没有它我也能活下去。手头的问题是关于将 wordpress 网站作为静态网站的子域托管时的重写规则。可以忽略 W3 total cache 配置部分。我托管过 mydomain.com/blog 这样的网站,但重写规则并未应用。到目前为止,我尝试了许多替代方案。如果有人将 Wordpress 实现为纯子目录而不是多站点子目录。他们可以提供成功的配置。

答案1

这并不能直接回答您的问题,但这可能是您未曾考虑过的更好选择。使用 Nginx 页面缓存,而不是使用插件进行缓存。这样做速度更快,因为您不必调用 PHP,从而消除了大量开销。

缺点是,除非您购买商业版 Nginx,否则很难使 Nginx 缓存失效。您可以使用可以完成此工作的插件构建 Nginx,但 Wordpress / Nginx 缓存集成效果不佳。我发现没有一个插件效果很好。因此,您需要仔细设置缓存的最大寿命。有趣的是,在繁忙的网站上,即使几秒钟的缓存也可以带来好处。我的网站很少更改,如果需要,我只需 rm -rf nginx 页面缓存所在的正确目录即可 - 它实际上在内存中。

我有一个关于这个的教程这里,还会有许多其他人。有一篇很棒的文章Nginx 微缓存

SF 喜欢页面上的实际数据,以防网站消失。

在你的 nginx.conf 中

fastcgi_cache_key "$scheme$request_method$host$request_uri";

在你的站点文件顶部,或者在你的 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

在调用 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;

我链接到的教程包含更多信息和解释。

答案2

您需要采取一些步骤来解决问题(从当前情况来看)......

在文件系统中...

看:https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory

  1. 将 WordPress 核心文件从/var/www/wordpress移动到/var/www/wordpress/blog。(提示mv /var/www/wordpress /var/www/blog && mkdir /var/www/wordpress && mv /var/www/blog /var/www/wordpress:)

  2. 复制/var/www/wordpress/blog/index.php到目录/var/www/wordpress/

  3. 编辑/var/www/wordpress/index.php文件并将行更改require( dirname( __FILE__ ) . '/wp-blog-header.php' );require( dirname( __FILE__ ) . '/blog/wp-blog-header.php' );

Nginx的:

现在,Nginx 部分。以下是相关部分的变化...

location /blog {           
    root /var/www/wordpress;
    index index.php;
    try_files $uri $uri/ /blog/index.php?q=$request_uri;                        
}   

location ~ \.php$ {             
    root /var/www/wordpress;
    index index.php;
    try_files $uri $uri/ /blog/index.php?q=$request_uri =404;

    # other configuration directives
}

主要有两个变化:

  1. alias指令变为root
  2. try_files指令包含/blog/之前index.php

我希望这有帮助!

相关内容