Nginx conf文件:从HTTP子域重定向到HTTPS端口

时间:2014-01-21 10:22:16

标签: ssl nginx

我是Nginx的新手,我在端口8000上设置了Ajenti的全新安装。

我希望在Ajenti有一个子域名,它是HTTPS,但不能在网站上的任何其他位置使用HTTPS。所有这些都发生了:

http://domain1.co.uk

http://sub.domain1.co.uk -> https://sub.domain1.co.uk

https://sub.domain1.co.uk -> https://domain1.co.uk:8000

我已经达到了:

http {
    index index.html;

    ssl_certificate      common.crt;
    ssl_certificate_key  common.key;

    server {
        listen 80;
        server_name www.domain1.co.uk;
        access_log logs/domain1.access.log main;

        root /var/www/domain1.co.uk/htdocs;
    }
    server {
        listen 80;
        server_name sub.domain1.co.uk;

        location / {
            proxy_pass https://127.0.0.1:8000/;
            proxy_set_header Host $host;
            proxy_buffering off;
        }
    }
}

我离我有多远?

提前致谢

马赫

2 个答案:

答案 0 :(得分:1)

如果我理解正确,那就是:

http {
    index index.html;

    ssl_certificate      common.crt;
    ssl_certificate_key  common.key;

    # Redirect http://sub.domain1.co.uk to https://sub.domain1.co.uk
    server {
        listen 80;
        server_name sub.domain1.co.uk;

        return 301 https://sub.domain1.co.uk;
    }
    # Proxy pass https://sub.domain1.co.uk to http://127.0.0.1:8000/
    server {
        listen 443;
        server_name https://sub.domain1.co.uk;
        location / {
            proxy_pass http://127.0.0.1:8000/;
            proxy_set_header Host $host;
            proxy_buffering off;
        }
    }
    # http://domain1.co.uk & www.domain1.co.uk
    server {
        listen 80;
        server_name domain1.co.uk
                    www.domain1.co.uk;
        access_log logs/domain1.access.log main;

        root /var/www/domain1.co.uk/htdocs;
    }        
}

答案 1 :(得分:1)

不得不做一些小调整,但最终到达那里。 HongTat不仅指向了正确的方向。这就是我最终的结果:

# Redirect http://sub.domain1.co.uk to https://sub.domain1.co.uk
server {
    listen 80;
    server_name sub.domain1.co.uk;

    return 301 https://sub.domain1.co.uk;
}

# Proxy pass https://sub.domain1.co.uk to http://127.0.0.1:8000/
server {
    listen 443;
    server_name sub.domain1.co.uk;

    client_max_body_size 20m;

    ssl on;
    ssl_certificate /etc/nginx/ssl/server.crt;
    ssl_certificate_key /etc/nginx/ssl/server.key;

    location / {
        proxy_pass https://127.0.0.1:8000/;
        proxy_set_header Host $host;
        proxy_buffering off;
    }
}

# http://domain1.co.uk & www.domain1.co.uk
server {
    listen 80;
    server_name domain1.co.uk
                www.domain1.co.uk;

    index index.html;

    root /var/www/domain1.co.uk/htdocs;
}

关注SSL的精彩指南 https://www.digitalocean.com/community/articles/how-to-create-a-ssl-certificate-on-nginx-for-ubuntu-12-04/

让我的第一个nginx设置按计划运行