我的 NGINX 服务器块是否设置不正确(HTTPS 301 和资产未在浏览器中缓存)?

我的 NGINX 服务器块是否设置不正确(HTTPS 301 和资产未在浏览器中缓存)?

我继承了一台服务器,并诊断出几个问题,且某些页面速度性能测试正在下降。

1) 在服务器块中,某些请求被重定向到https://25parkrow.com。例如,如果您只输入wwww.25parkrow.com或,25parkrow.com就会发生重定向。但如果您输入,https://25parkrow.com则不会发生重定向。如您所见,重定向发生在包含的服务器块中。这是可以接受的做法吗?如果不是,我该如何避免这些重定向?

2) 我在设置静态资产的过期标头时遇到问题。如果我更新服务器上的文件并重新加载 nginx 服务器,浏览器将看不到更新的资产。

我读了这篇文章:为 nginx 提供的静态内容设置过期标头。然而这让我很困惑,因为我不知道应该把缓存头放在下面的服务器块的什么位置。

nginx.conf

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

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

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

/etc/nginx/sites-enabled/active(仅一个文件处于活动状态):

server {
  add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

  root /var/www/html;

  # Add index.php to the list if you are using PHP
  index index.html index.htm index.nginx-debian.html;

  server_name 25parkrow.com www.25parkrow.com;

  # Any route that doesn't have a file extension (e.g. /devices)
  location / {
    try_files $uri $uri/ /index.html;
  }

  if ($host = www.25parkrow.com) {
    rewrite ^(.*) https://25parkrow.com$request_uri;
  }

  listen 443 ssl; # managed by Certbot
  ssl_certificate /etc/letsencrypt/live/25parkrow.com/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/25parkrow.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 {
  listen 80 default_server;
  server_name 25parkrow.com www.25parkrow.com;

  if ($host = www.25parkrow.com) {
      return 301 https://$host$request_uri;
  } # managed by Certbot


  if ($host = 25parkrow.com) {
      return 301 https://$host$request_uri;
  } # managed by Certbot

  return 404; # managed by Certbot
}

答案1

  1. 预计在安装 LetsEncrypt 证书时,您选择将 http 重定向到 https。您可以在 nginx 配置文件中注释重定向以禁用此功能。

  2. 在您现有的 nginx 配置中,http 被重定向到 https。因此,请确保在 https 服务器块中配置“expire headers”。

相关内容