Nginx有条件重定向到https [英] Nginx conditional redirect to https

查看:80
本文介绍了Nginx有条件重定向到https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,尽管所有流量都应将http重定向到https,除非上下文为/publish

I have a situation where although all the traffic should redirect http to https except when the context is /publish

http {mydomain.com/*}应该强制重定向到https {mydomain.com},但如果URL是http {mydomain.com/publish},则不会强制重定向.

http{mydomain.com/*} should force redirect to https{mydomain.com} but not in case if the url is http{mydomain.com/publish}, no redirect needed.

预先感谢

推荐答案

在nginx中进行重定向的最佳方法是使用多个server{}块. 你应该可以这样:

The best way to do redirect in nginx is using multiple server{} blocks. You should be fine with something like that:

server {
  server_name mydomain.com;
  listen 80;

  location / {
    return 301 https://$server_name$request_uri;
  }
  location /publish {
    # Here goes your usual request handling, with proxying and so on
    # Nested location can be used if needed
  }
}

server {
  server_name mydomain.com;
  listen 443;
  ssl on;
  # Here goes all your request handling 
}

这篇关于Nginx有条件重定向到https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆