将所有内容redirect到HTTPS时,也允许某些URL在HTTP上可用

现在,我使用下面的代码在我的nginxconfiguration中将所有HTTP URLredirect到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; } } 

如何使资产目录下的文件(recursion)通过HTTP可用?

 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, eg: include fastcgi_params; fastcgi_pass /var/run/fastcgi.sock; } 

假设您需要/资产可以同时通过HTTP和HTTPS访问,

你需要这样的东西:

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