Nginx 条件重定向到 https

标签 nginx

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

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

提前致谢

最佳答案

在 nginx 中进行重定向的最佳方法是使用多个 server{}块。
你应该没问题:

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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40181475/

相关文章:

python - 如何处理 Flask 上的静态/媒体文件权限?

nginx - Docker的Openresty Hello World

ruby - Ubuntu 上的 Nginx 设置

nginx - 无法使用 --from-code 选项创建 OpenShift 应用程序

Nginx 规则阻止特定 URL

python - 如何在 CentOS 上启动 redis 服务器并配置 nginx 以运行 mediacrush 脚本?

docker - Nginx反向代理到HTTPS上游获得502 Bad Gateway?

css - nginx 或更少输出未知字符

具有非标准端口的 GitLab 远程存储库

ssl - Nginx PEM_read_bio_X509 PEM_read_bio :bad end line