使用 nginx 通过 HTTPS 访问 www 子域时出现错误请求

使用 nginx 通过 HTTPS 访问 www 子域时出现错误请求

我没有问题,无需万维网

这是我的配置文件:

server {

listen 443 ssl;
server_name .terralmarket.es;
proxy_set_header Host $http_host;
ssl_certificate /etc/letsencrypt/live/terralmarket.es/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/terralmarket.es/privkey.pem;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/ssl/certs/dhparam.pem;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
add_header Strict-Transport-Security max-age=15768000;

location /static {
    autoindex on;
    alias /home/terralmarket/static;
}

location /media {
    autoindex on;
    alias /home/terralmarket/media;
}

location / {
        proxy_pass http://localhost:8003/;
}

}

server {
    listen 80;
    server_name .terralmarket.es;
    return 301 https://$server_name$request_uri;
}

知道我做错了什么吗? 我也尝试过server_name按域分隔。

答案1

正如您所发现的,您需要为 Nginx 设置不同的位置块,以便转发到您的根域。您已确定了一个,下面是另一个。它们将所有内容转发到http://example.com域,但你可以按照你喜欢的方式调整它们

# Main server
server {
  listen 443 ssl http2;
  server_name example.com;
  # etc, locations, etc
}

# Forward non-www requests to www
server {
  listen       80;
  server_name  example.com www.example.com;
  access_log  /var/log/nginx/access.log main buffer=128k flush=1m if=$log_ua;
  return       301 https://www.example.com$request_uri;
}

# Forward https://www.example.com to https://example.com
server {
  listen 443 ssl http2;
  server_name www.example.com;

  ssl_certificate /var/lib/acme/certs/***CERT_DIRECTORY/fullchain;
  ssl_certificate_key /var/lib/acme/certs/***CERT_DIRECTORY/privkey;

  # Set up preferred protocols and ciphers. TLS1.2 is required for HTTP/2
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;

  access_log  /var/log/nginx/access.log main buffer=128k flush=1m if=$log_ua;

  return 301 https://www.example.com$request_uri;
}

相关内容