大网数据广告活动
服务器运维问答

Redirect non-www to www over SSL with Nginx

· 52CE 我爱测

I'm having an error when trying to redirect https://example.com to https://www.example.com . When I go to https://example.com , it doesn't redirect and returns the page/200 status. I don't want this, I want it to redirect to http…

问题原文

原帖链接 · Thomas V. · CC BY-SA 3.0

I'm having an error when trying to redirect https://example.com to https://www.example.com.

When I go to https://example.com, it doesn't redirect and returns the page/200 status.

I don't want this, I want it to redirect to https://www.example.com.

When I go to http://example.com, it redirects to https://www.example.com

Can somebody tell me where I am going wrong?

This is my default and default-ssl configuration files:

default.conf

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

default-ssl.conf

upstream app_server_ssl {
    server unix:/tmp/unicorn.sock fail_timeout=0;
}

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

    listen 443;
    root /home/app/myproject/current/public;
    index index.html index.htm;

    error_log /srv/www/example.com/logs/error.log info;
    access_log /srv/www/example.com/logs/access.log combined;

    ssl on;
    ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    ssl_certificate /srv/www/example.com/keys/ssl.crt;
    ssl_certificate_key /srv/www/example.com/keys/www.example.com.key;
    ssl_ciphers AES128-SHA:RC4-MD5:ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:RSA+3DES:!ADH:!AECDH:!MD5:AES128-SHA;
    ssl_prefer_server_ciphers on;

    client_max_body_size 20M;


    try_files $uri/index.html $uri.html $uri @app;


    # CVE-2013-2028 http://mailman.nginx.org/pipermail/nginx-announce/2013/000112.html
    if ($http_transfer_encoding ~* chunked) {
            return 444;
        }

    location @app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server_ssl;
    }

    error_page 500 502 503 504 /500.html;

    location = /500.html {
        root /home/app/example/current/public;
    }
}

已采纳解答

原帖链接 · masegaloeh · CC BY-SA 3.0

You are missing listen directive in file default-ssl.conf. Add listen 443; in this directive

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

By default, if you omit this directive, nginx assume that you want listen on port 80. Here the documentation of this default behavior.


Edit: Thanks for comment from @TeroKilkanen.

Here the complete config for your default-ssl.conf

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /srv/www/example.com/keys/ssl.crt;
    ssl_certificate_key /srv/www/example.com/keys/www.example.com.key;
    return 301 https://www.example.com$request_uri;
}

Sidenote: You can replace ssl on; directive with listen 443 ssl; as recommendation from nginx documentation.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2014-08-28)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。