【问题标题】:nginx www domain does not work when redirecting重定向时nginx www域不起作用
【发布时间】:2020-07-18 02:02:02
【问题描述】:

我在使用 http://www.example.com 重定向内容时遇到问题。换句话说,http://example 可以正常工作并重定向到 https,但 http://www.example.com 不能。

下面是我的 nginx 配置

server {
  listen 80;
  listen [::]:80;
  server_name example.com;
  return 301 https://www.example.com$request_uri;
}

server {
 listen 80;
 listen [::]:80;
 server_name www.example.com;
 return 301 https://www.example.com$request_uri;
}

server {

listen 443 ssl http2;
server_name www.example.com;

sendfile on;

ssl_certificate /usr/share/example_chain.crt;
ssl_certificate_key /usr/share/7a45ae3816f4bc8f.pem;
default_type application/octet-stream;

gzip on;
gzip_http_version 1.1;
gzip_disable      "MSIE [1-6]\.";
gzip_min_length   256;
gzip_vary         on;
gzip_proxied      expired no-cache no-store private auth;
gzip_types        text/plain text/css application/json application/javascript application/x- 
javascript text/xml application/xml application/xml+rss text/javascript;
gzip_comp_level   9;

root /usr/share/nginx/html;

location / {
  try_files $uri $uri/ /index.html =404;
 }

}

此外,我有一个 docker 容器,它为我的 ec2 实例上的端口 443 到 docker 容器内的端口 443 提供请求。

在我有这个之前 ->

server {
  listen 80;
  listen [::]:80;
  server_name www.example.com example.com;
  return 301 https://www.example.com$request_uri;
}

【问题讨论】:

    标签: nginx web dns


    【解决方案1】:

    您可以使用 nginx 中的 IF 模块来解决此问题。参考下面的配置。

    server {
             listen 80;
             server_name example.com www.example.com;
             return 301 https://www.example.com$request_uri;
    }
    
    server {
            listen 443;
            server_name example.com;
            root /var/www/html;
            index index.html index.php;
    
            location / {
               try_files $uri $uri/ /index.php?$args;
            }
    
    
            ssl on;
            ssl_certificate /etc/nginx/ssl/cert.crt;
            ssl_certificate_key /etc/nginx/ssl/key.key;
            ssl_session_timeout 5m;
    
            if ($scheme = http) {
                     rewrite ^/(.*)$ https://www.example.com/$1 permanent;
             }
    
             if ($host !~* ^www\.) {
                     rewrite ^/(.*)$ https://www.example.com/$1 permanent;
             }
    
    
    }
    

    【讨论】:

    • 我仍然遇到问题 :( 非常感谢您的帮助!我正在输入 (http) www.example.com,我希望它转到 (https) www.example .com
    • 您可以 curl -I example.co 并在此处发布您收到的 http 响应。
    • 这是使用上面的配置! -> HTTP/1.1 301 永久移动服务器:nginx/1.12.2 日期:星期三,2020 年 4 月 8 日 08:04:58 GMT 内容类型:文本/html; charset=utf-8 连接:关闭位置:(https)://www.example.com
    • 实际上我包含了这个 -> if ($host ~* ^www.example.com$) { return 301 (https)://www.example.com$request_uri; } 在我的服务器中 { 听 80 }
    • @AurelianoYepez 如果您使用 $host ~* ^www.example.com$ Nginx 将重定向仅带有 www 的请求。相反,使用这个 if ($host !~* ^www\.) 这将被修复。
    猜你喜欢
    • 1970-01-01
    • 2014-02-04
    • 2017-11-21
    • 2020-12-31
    • 2017-01-11
    • 1970-01-01
    • 2017-09-29
    • 2015-06-09
    相关资源
    最近更新 更多