NGINX - HTTP / 2中反向代理的静态内容

时间:2018-03-07 10:02:16

标签: nginx reverse-proxy http2 proxypass

我有一个node.js应用程序,它通过NGINX中的proxy_pass提供,我启用了HTTP / 2。当然,反向调用是在HTTP / 1.1中,我添加了一个块来直接提供静态资产,而没有反向代理,所以它们应该在HTTP / 2中,但它们没有。

这是我的代码:

# Expires map
map $sent_http_content_type $expires {
    default                    off;
    text/html                  epoch;
    text/css                   max;
    application/javascript     max;
    ~image/                    max;
}

# The upstream module is the link between Node.js and Nginx.
upstream balance {
  # Set up multiple Node.js webservers for load balancing.
  server 127.0.0.1:3333 max_fails=0 fail_timeout=10s weight=1;

  # Send visitors back to the same server each time.
  ip_hash;

  # Enable number of keep-alive connections.
  keepalive 512;
}

server {
  listen 443 ssl http2 default_server; # managed by Certbot
  listen [::]:443 ssl http2 ipv6only=on default_server; # managed by Certbot

  ssl_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/mysite.com/privkey.pem; # managed by Certbot
  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

  server_name mysite.com www.mysite.com;

  expires $expires;

  # Set cookie size
  large_client_header_buffers 4 64k;
  fastcgi_buffers 16 64k;
  fastcgi_buffer_size 64k;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;

  # Serve static files without going through upstreams
  location ~ ^/(assets/images/|assets/js/|assets/css/|static/|robots.txt|humans.txt|favicon.ico) {
    root /var/www/project/public;
    access_log off;
    expires max;
  }

  location / {
    # Set this to your upstream module.
    proxy_pass http://balance;

    # Headers to pass to proxy server.
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto-Version $http2;
    proxy_set_header Host $host;
    proxy_set_header X-NginX-Proxy true;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_cache_bypass $http_upgrade;
    proxy_http_version 1.1;
    proxy_redirect off;
    # Go to next upstream after if server down.
    proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
    proxy_connect_timeout 5s;
    # Gateway timeout.
    proxy_read_timeout 20s;
    proxy_send_timeout 20s;
    # Buffer settings.
    proxy_buffers 8 32k;
    proxy_buffer_size 64k;
  }
}

相对于静态资产的块正常工作,但通过该块的所有调用始终是HTTP / 1.1而不是HTTP / 2:

example http1

知道原因吗?

这是nginx -V的输出,它包含标记--with-http_v2_module

nginx version: nginx/1.10.3 (Ubuntu)
built with OpenSSL 1.0.2g  1 Mar 2016
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads

0 个答案:

没有答案