将所有内容重定向到 HTTPS 时,允许某些 URL 也可用于 HTTP

将所有内容重定向到 HTTPS 时,允许某些 URL 也可用于 HTTP

现在,我使用 nginx 配置中的以下代码将所有 HTTP URL 重定向到 HTTPS:

server {
  server_name domain.com
  listen 80;

  location /asset/ {
    # make files under asset directory (recursively) be available over HTTP too
  }

  location / {
    rewrite ^ https://$server_name$request_uri? permanent;
  }
}

我怎样才能使资产目录下的文件(递归地)也通过 HTTP 提供?

答案1

server {
    listen 80;
    server_name domain.com;
    root /var/www;

    location / {
        location /asset {
            try_files $uri =404;
        }
        return 301 https://$server_name$request_uri;
    }
}

如果您想将丢失的文件传递给您的应用程序,您可以执行以下操作:

location / {
    location /asset {
        try_files $uri @app;
    }
    return 301 https://$server_name$request_uri;
}

location @app {
    # Add whatever logic you'd like to perform, e.g.:
    include fastcgi_params;
    fastcgi_pass /var/run/fastcgi.sock;
}

答案2

假设你需要 /asset 同时通过 HTTP 和 HTTPS 访问,

你需要这样的东西:

# HTTPS server {} section
# HTTP server {} section 
location !~ ^/asset {
   rewrite (.*) https://$server_name$1 permanent;
}

相关内容