已加载Ngx_pagespeed,但在HTTPS网站上不执行任何操作。这是为什么?

时间:2014-10-11 12:11:23

标签: wordpress nginx https webserver pagespeed

模块ngx_pagespeed(Nginx)非常适合HTTP。但是,我无法使用HTTPS。我的整个网站都使用HTTPS,而ngx_pagespeed似乎没有任何过滤器正常工作。模块本身已加载,但什么都不做。我在使用CentOS 7上最新的ngx_pagespeed模块的网站上使用WordPress。

这是我的nginx.conf

user  nginx nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /run/nginx.pid;


events {
    use epoll;
  worker_connections 1024;
  multi_accept on;
}


http {

        ##
        # MIME types
        ##
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

        # Update charset_types due to updated mime.types
        charset_types text/xml text/plain text/vnd.wap.wml application/x-javascript application/rss+xml text/css application/javascript application/json;


        ##
        # Misc
        ##
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

        client_max_body_size 20m;
        client_body_buffer_size 128k;
        client_body_timeout 15;
        client_header_timeout 15;
        keepalive_timeout 65;
        reset_timedout_connection on;
        send_timeout 15;
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;

      open_file_cache max=1000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;

        server_tokens off;

        ##
        # Logging Settings
        ##
    access_log  /var/log/nginx/access.log  main;

        ##
        # Gzip Settings - Ngx_pagespeed to by default.
        ##
        # gzip            on;
        # gzip_min_length 256;
        # gzip_comp_level 4;
        # gzip_proxied    any;
        # gzip_vary             on;
        # gzip_types
        #     application/atom+xml
        #     application/javascript
        #     application/json
        #     application/rss+xml
        #     application/vnd.ms-fontobject
        #     application/x-font-ttf
        #     application/x-web-app-manifest+json
        #     application/xhtml+xml
        #     application/xml
        #     font/opentype
        #     image/svg+xml
        #     image/x-icon
        #     text/css
        #     text/plain
        #     text/x-component;

        ## Enable clickjacking protection in modern browsers.
    ## https://developer.mozilla.org/en/The_X-FRAME-OPTIONS_response_header
    add_header X-Frame-Options sameorigin;

        ##
        # Host Configs
        ##
    include /etc/nginx/conf.d/*.conf;
}

这是我的example.conf

##
# WWW to NON-WWW
##
server {
    listen 80;
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

##
# Force HTTPS
##
server {
    listen  80;
    server_name example.com;
    return 301 https://example.com$request_uri;
}

##
# The right way to add support for HSTS.
# http://trac.nginx.org/nginx/ticket/289
##
map $scheme $hsts_header {
    https max-age=31536000;
}

##
# Phuchan site
##
server {
    listen 443 ssl spdy;

  # Certs sent to the client in SERVER HELLO are concatenated in ssl_certificate.
    ssl on;
    ssl_certificate /etc/ssl/certs/ssl-bundle.crt;
    ssl_certificate_key /etc/ssl/private/myserver.key;

  # Diffie-Hellman parameter for DHE ciphersuites, recommended 2048 bits.
    ssl_dhparam /etc/ssl/certs/dhparam.pem;

    # Intermediate configuration.
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers 'AES256+EECDH:AES256+EDH';
    ssl_prefer_server_ciphers on;
    ssl_session_cache  builtin:1000  shared:SSL:10m;
    ssl_session_timeout 10m;

    # OCSP Stapling
    ssl_stapling on;
    ssl_stapling_verify on;
  ssl_trusted_certificate /etc/ssl/certs/trustchain.crt;
    resolver 8.8.8.8 8.8.4.4 valid=300s;

    # PageSpeed
    pagespeed on;
    pagespeed FetchHttps enable;
    #pagespeed MapOriginDomain "http://localhost" "https://example.com";

    # Needs to exist and be writable by nginx.  Use tmpfs for best performance.
    pagespeed FileCachePath /var/ngx_pagespeed_cache;

    # Ensure requests for pagespeed optimized resources go to the pagespeed handler
    # and no extraneous headers get set.
    location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
     add_header "" "";
    }
    location ~ "^/pagespeed_static/" { }
    location ~ "^/ngx_pagespeed_beacon$" { }

    # Honoring no-transform Cache-Control Headers
    pagespeed DisableRewriteOnNoTransform off;

    # Lower-casing HTML element and attribute names
    pagespeed LowercaseHtmlNames on;
    pagespeed RewriteLevel OptimizeForBandwidth;

    # Preserve URL Relativity
    pagespeed PreserveUrlRelativity on;


    # Misc
    add_header Strict-Transport-Security $hsts_header;
  add_header X-Content-Type-Options nosniff;

    server_name example.com;
    root /srv/www/example.com;

    index index.php index.htm index.html;
    error_log /var/log/nginx/error-example.log error;

  location / {
        # This is cool because no php is touched for static content.
        # include the "?$args" part so non-default permalinks doesn't break when using query string
        try_files $uri $uri/ /index.php?$args;
  }

    ##
    # PHP-FPM
    ##
    location ~ \.php$ {
        try_files      $uri = 404;
        fastcgi_pass   unix:/run/php-fpm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    ##
    # Server the assets folder
    ##
    location ^~ /assets {
        alias /srv/assets;
    }

    ##
    # Simple cache for static files. Tweaked for SSL use.
    ##
    location ~ \.(js|css|png|jpeg|jpg|gif|ico|swf|flv|pdf|zip)$ {
        expires 24h;
        add_header Cache-Control public;
    }

    ##
    # WordPress stuff
    ##
  location = /favicon.ico {
        log_not_found off;
        access_log off;
  }

  location = /robots.txt {
        try_files $uri $uri/ /index.php?$args;
        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 ~* /(?:uploads|files)/.*\.php$ {
        deny all;
    }

  # Set variable $skip_cache to 0
  set $skip_cache 0;

    # Do not cache POST/HEAD requests
    if ($request_method ~ ^(HEAD|POST)$) {
        set $skip_cache 1;
    }

  # Do not cache URLs with a query string
    if ($query_string != "") {
        set $skip_cache 1;
    }

    # Do not cache URLs containing the following segments
    if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
        set $skip_cache 1;
    }

    # Do not cache logged in users or recent commenters
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
        set $skip_cache 1;
    }

    ##
    # Rewrite for XML Sitemap Generator
    ##
    rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.xml$ "/index.php?xml_sitemap=params=$2" last;
    rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.xml\.gz$ "/index.php?xml_sitemap=params=$2;zip=true" last;
    rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.html$ "/index.php?xml_sitemap=params=$2;html=true" last;
    rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.html.gz$ "/index.php?xml_sitemap=params=$2;html=true;zip=true" last;

}

2 个答案:

答案 0 :(得分:1)

您必须提供特定于https的配置才能重写https资源。

来自https://developers.google.com/speed/pagespeed/module/https_support

PageSpeed重写通过https请求的HTML文档。 PageSpeed能够提供这些文档,因为服务器通过其所有输出过滤器传递HTML文档,包括* _pagespeed。但默认情况下,PageSpeed只会重写通过http提供的非HTML资源。由于管理客户端SSL证书所需的复杂性和安全性,PageSpeed要求服务器管理员明确启用https提取。

https://developers.google.com/speed/pagespeed/module/https_support提供了有关在不同情况下所需配置的更多详细信息。

答案 1 :(得分:1)

我用https://developers.google.com/speed/pagespeed/module/https_support#load_from_file解决了这个问题。第二个参数应该指向您网站的根目录。