这是配置 nginx 以准备使用 varnish-cache 的正确方法吗?

这是配置 nginx 以准备使用 varnish-cache 的正确方法吗?

语境

我使用 bitnami 预装镜像在 Google Compute 上启动了一个 nginx+wordpress 网站。我想varnish-cache在此服务器上启用。varnish 4.x 包已预装但尚未启用。

据我所知,使用过程分为两个步骤varnish-cache

1)让 wordpress 的 ssl 部分与服务的 http 端口对话(而不是直接调用 fastcgi)

2) 将 varnish 端口设置为 80,并通过另一个端口(例如 8080)与 nginx 通信。让 nginx 服务器对 wordperess 使用 8080 而不是 80。

我正在尝试通过更改 wordpress/nginx 配置的这一部分来实现第一步

 server {


     listen    443 ssl;

      include "/opt/bitnami/apps/wordpress/conf/nginx-app.conf";

服务器 {

 listen    443 ssl;

   location / {
     proxy_pass http://127.0.0.1:80;
     proxy_set_header Host $http_host;
     proxy_set_header X-Forwarded-Host $http_host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header X-Forwarded-Proto https;
     proxy_set_header HTTPS "on";


     access_log /opt/bitnami/nginx/logs/access-https.log;
 }

添加额外访问日志的想法是为了测试验证 nginx 是否采用了新的配置。

但事实似乎并非如此。实际上,即使我将 http 端口更改为无效端口(例如proxy_pass http://127.0.0.1:79;),在重新启动 nginx 服务器后,wordpress 网站仍可正常运行。

你能说出我的上述改变有什么问题吗?

服务器上原有的nginx配置:

./wordpress/conf/nginx-app.conf

index index.php index.html index.htm;
if ($request_uri !~ "^/phpmyadmin.*$")
{
  set $test  A;
}
if ($request_uri !~ "^/bitnami.*$")
{
  set $test  "${test}B";
}
if (!-e $request_filename)
{
  set $test  "${test}C";
}
if ($test = ABC) {
  rewrite ^/(.+)$ /index.php?q=$1 last;
}
include "/opt/bitnami/apps/bitnami/banner/conf/banner-substitutions.conf";
include "/opt/bitnami/apps/bitnami/banner/conf/banner.conf";
location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_read_timeout 300;
    fastcgi_pass unix:/opt/bitnami/php/var/run/www.sock;
    fastcgi_index index.php;
    fastcgi_param  SCRIPT_FILENAME $request_filename;
    include fastcgi_params;
}

./wordpress/conf/nginx-vhosts.conf

server {
    listen    80;
    root   "/opt/bitnami/apps/wordpress/htdocs";
    server_name  wordpress.example.com www.wordpress.example.com;

    include "/opt/bitnami/apps/wordpress/conf/nginx-app.conf";
}
server { 
    listen    443 ssl;
    root   "/opt/bitnami/apps/wordpress/htdocs";
    server_name  wordpress.example.com www.wordpress.example.com;
    ssl_certificate      "/opt/bitnami/apps/wordpress/conf/certs/server.crt";
    ssl_certificate_key  "/opt/bitnami/apps/wordpress/conf/certs/server.key";

       ssl_session_cache    shared:SSL:1m;
       ssl_session_timeout  5m;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    include "/opt/bitnami/apps/wordpress/conf/nginx-app.conf";
}

./wordpress/conf/nginx-prefix.conf

root "/opt/bitnami/apps/wordpress/htdocs";
include "/opt/bitnami/apps/wordpress/conf/nginx-app.conf";

./nginx/conf/nginx.conf

user  daemon daemon;
worker_processes  auto;
error_log  "/opt/bitnami/nginx/logs/error.log";
pid        "/opt/bitnami/nginx/logs/nginx.pid";
events {
    use                 epoll;
    worker_connections  1024;
    multi_accept        on;
}
http {
    client_max_body_size 8M;
    include       mime.types;
    default_type  application/octet-stream;
    client_body_temp_path  "/opt/bitnami/nginx/tmp/client_body" 1 2;
    proxy_temp_path "/opt/bitnami/nginx/tmp/proxy" 1 2;
    fastcgi_temp_path "/opt/bitnami/nginx/tmp/fastcgi" 1 2;
    scgi_temp_path "/opt/bitnami/nginx/tmp/scgi" 1 2;
    uwsgi_temp_path "/opt/bitnami/nginx/tmp/uwsgi" 1 2;
    access_log  "/opt/bitnami/nginx/logs/access.log";
    sendfile        on;
    keepalive_timeout  65;
    gzip on;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_vary on;
    gzip_types text/plain
               text/xml
               text/css
               text/javascript
               application/json
               application/javascript
               application/x-javascript
               application/ecmascript
               application/xml
               application/rss+xml
               application/atom+xml
               application/rdf+xml
               application/xml+rss
               application/xhtml+xml
               application/x-font-ttf
               application/x-font-opentype
               application/vnd.ms-fontobject
               image/svg+xml
               image/x-icon
               application/atom_xml;
    gzip_buffers 16 8k;
    add_header X-Frame-Options SAMEORIGIN;
    ssl_prefer_server_ciphers  on;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS;
    include "/opt/bitnami/nginx/conf/bitnami/bitnami.conf";
}

Nginx 版本

nginx -V
nginx version: nginx/1.12.2
built with OpenSSL 1.0.2o  27 Mar 2018
TLS SNI support enabled
configure arguments: --prefix=/bitnami/lampstack-linux-x64/output/nginx --with-http_sub_module --with-http_ssl_module --with-http_stub_status_module --w
ith-cc-opt=-I/bitnami/lampstack-linux-x64/output/common --with-ld-opt=-L/bitnami/lampstack-linux-x64/output/common/lib --with-http_gzip_static_module --
with-mail --with-mail_ssl_module --with-http_realip_module --with-http_stub_status_module --with-http_v2_module

答案1

443中的这部分server将覆盖您的更改:

  include "/opt/bitnami/apps/wordpress/conf/nginx-app.conf";

server { listen 443 ... }您确定用自己的块替换了整个块吗?

你可能已经在使用其他https 前端 - 例如 CDN(CloudFlare 等)。使用host mydomain.com或检查您的域名dig mydomain.com如何nslookup mydomain.com解析为互联网上的 IP。检查并重新配置在该 IP 上监听端口 443 的任何内容 - 很可能是您的 nginx 以外的其他内容。也许它已经在缓存您,在这种情况下您不需要 varnish。

相关内容