nginx将所有http重定向到https,但有一个例外
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了nginx将所有http重定向到https,但有一个例外,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2034字,纯文字阅读大概需要3分钟。
内容图文
![nginx将所有http重定向到https,但有一个例外](/upload/InfoBanner/zyjiaocheng/966/5b817e2379e74809ac853c37a4c63b5b.jpg)
我想将所有http流量重定向到https,但有一个例外.在网址中有/ preview /的任何内容我想保留在http上.
我已尝试使用以下配置,但它一直告诉我,我有一个重定向循环.
server {
listen 80;
server_name example.com;
root /var/www/html/example.com/public;
index index.php index.html;
location /preview {
try_files $uri $uri/ /index.php?$query_string;
}
location / {
# we are in http server, but want https for normal
# requests - redirect to https
return 301 https://$server_name$request_uri;
}
location ~ \.php${
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
#fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 443;
server_name example.com;
ssl on;
ssl_certificate /etc/nginx/ssl/cert_chain.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
add_header Strict-Transport-Security max-age=31536000;
access_log /var/log/nginx/example.com/access.log;
error_log /var/log/nginx/example.com/error.log;
charset utf-8;
root /var/www/html/example.com/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location /preview {
# we are in http server, but want https for normal
# requests - redirect to https
return 301 http://$server_name$request_uri;
}
location ~ \.php${
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
#fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
解决方法:
为了确保您在任何情况下都不会在/ preview中发布任何重定向,您可能希望始终使重定向成为有条件的,例如:
listen 80;
location / {
if ($request_uri !~ /preview) {
return 302 https://...;
}
}
内容总结
以上是互联网集市为您收集整理的nginx将所有http重定向到https,但有一个例外全部内容,希望文章能够帮你解决nginx将所有http重定向到https,但有一个例外所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。