当前位置: > Linux服务器 > nginx >

Nginx 配置SSL并且完成80到8080的反向代理

时间:2016-02-22 15:31来源:linux.it.net.cn 作者:IT
server {
        listen 80;
        server_name coddict.tk;
        rewrite ^(.*)$ https://$host$1 permanent;
}
server {
        listen 443;
        server_name coddict.tk;
        ssl on;
        ssl_certificate         /etc/nginx/ssl/nginx.crt;
        ssl_certificate_key     /etc/nginx/ssl/nginx.key;
        location / {
                proxy_pass https://127.0.0.1:8080;
        }
}
上面是我新写的一个nginx的配置,我现在有一个go程序跑在网上,占用的端口是8080,之前没有用ssl的时候是可以从80反向代理到8080的,当然配置也不是上面这样的。现在加了SSL之后,就必须要通过https://www.coddickt.tk才能进行访问了。80那边监听的也用rewrite重写了所有http请求到https。为什么还是不能直接输入www.coddict.tk就是https请求呢?


server {
        listen       80;
        server_name  coddict.tk;
        rewrite ^(.*)$ https://$host$1 permanent;
    }

    server {
        listen       443;
        server_name  coddict.tk;

        ssl                  on;
        ssl_certificate      cert.pem;
        ssl_certificate_key  cert.key;

        location / {
            proxy_pass https://127.0.0.1:8080;
        }
    }

    server {
        listen       8080;
        server_name  coddict.tk;

        ssl                  on;
        ssl_certificate      cert.pem;
        ssl_certificate_key  cert.key;

        location / {
            root   /usr/share/nginx/html/ssl_8080;
            index  index.html index.htm;
        }
    }


我测试这样,访问http://www.coddict.tk

返回的URL是:https://www.coddict.tk/

返回的内容是:https://www.coddict.tk:8080/

测试没任何问题。


?
1
2
3
4
5
6
7
8
9
10
11
12
[lvfifi@archlinux ~]$ curl -I http://www.coddict.tk
HTTP/1.1 301 Moved Permanently
Server: nginx/1.6.1
Date: Sun, 10 Aug 2014 07:16:24 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: https://www.coddict.tk/

[lvfifi@archlinux ~]$ curl  -kL http://www.coddict.tk
ssl 8080
[lvfifi@archlinux ~]$ 













(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容