nginx - 使用相同的规则在多个端口上运行 nginx

标签 nginx

我在两个端口上运行 nginx,一个用于 HTTPS,另一个用于 HTTP,但在所有其他方面都相同(即 location 指令等)。我想避免重复编写相同的规则。

nginx 配置文件中有没有办法将我的规则写入某种 block 中,然后将它们包含在我的 server 指令中?

最佳答案

是的,您可以在配置中包含额外的文件,请参阅 docs示例如下:

server {
  listen              80;
  include             location_directives.conf
}

server {
  ssl                 on;
  listen              443 default spdy ssl;
  include             location_directives.conf
}

附注顺便说一句,它更喜欢使用 return 301 从 http 重定向到 https,而不是为两个服务器 block 定义规则

server {
  listen              80;
  return              301 https://$host$request_uri;
}

关于nginx - 使用相同的规则在多个端口上运行 nginx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25199342/

相关文章:

amazon-web-services - Api Gateway 与 Elastic Beanstalk 的连接(客户端 SSL 证书)

带有 nginx 设置主机头的 ASP.Net Core 不起作用

php - 状态 404 : ZF3 and Nginx

amazon-s3 - 什么是 RESTful Web 服务最具可扩展性和高性能的 Amazon Web Service (AWS) 配置?

c++ - 每个 cpp 的 nginx 语法高亮显示,无需人工交互

nginx - nginx 中的 "shared memory zone"是什么意思?

apache - 更改 Apache .htaccess 文件以与 Nginx 一起使用

php - laravel 的 nginx 配置

nginx - 为什么我在访问域时收到 400 Bad Request 错误?

php - 失败(104 : Connection reset by peer)