如何为 http 和 https 请求设置 Nginx

如何为 http 和 https 请求设置 Nginx

我需要设置 Nginx 以便它响应 http 和 https 请求;我这样配置了我的默认conf:

server {
  listen       80;
  listen       443;
  ssl           on;
  ssl_certificate       /etc/nginx/certs/domain-bundle.crt;
  ssl_certificate_key   /etc/nginx/certs/domain.it.key;
  server_name  static.domain.it;
  location / {
    try_files $uri @s3cache;
  }

  location @s3cache{
    proxy_cache            S3CACHE;
    proxy_cache_valid      200 48h;
    proxy_cache_valid      403 60m;
    proxy_pass http://static.domain.it.s3-external-3.amazonaws.com;
  }
}

https 协议工作正常,但如果我发出 http 请求,则会收到此错误:

400 Bad Request
The plain HTTP request was sent to HTTPS port

答案1

安全套接字层指令为整个虚拟主机启用 SSL,并且不查看端口号。如果要将 SSL 限制为单个端口,请替换:

  listen       80;
  listen       443;
  ssl           on;

经过:

  listen       80;
  listen       443 ssl;

http://nginx.org/r/listen

相关内容