Contents
  1. 1. 1、首先,进入你想创建证书和私钥的目录,例如:
  2. 2. 2、创建服务器私钥,命令会让你输入一个密码,然后重复验证密码:
  3. 3. 3、创建签名请求的证书(CSR),命令依次会你让输入密码、国家、省份、城市、组织名字、主机地址、邮箱,然后是挑战密码和公司名称,这两个不用输入,直接回车:
  4. 4. 4、在加载SSL支持的Nginx并使用上述私钥时除去必须的口令:
  5. 5. 5. 编辑nginx.conf 或者是 default.conf ,增加https代理
  6. 6. 6. 错误列表

用Nginx代理https,跟代理http的方式差不多,只是多了个SSL证书。
SSL证书可以自己生成,也可以从第三方机构购买,自己生成的证书是不受浏览器信任的。
自己生成不受浏览器信任的SSL证书

1、首先,进入你想创建证书和私钥的目录,例如:

cd /etc/nginx/

2、创建服务器私钥,命令会让你输入一个密码,然后重复验证密码:

openssl genrsa -des3 -out server.key 1024

3、创建签名请求的证书(CSR),命令依次会你让输入密码、国家、省份、城市、组织名字、主机地址、邮箱,然后是挑战密码和公司名称,这两个不用输入,直接回车:

openssl req -new -key server.key -out server.csr

4、在加载SSL支持的Nginx并使用上述私钥时除去必须的口令:

cp server.key server.key.com
openssl rsa -in server.key.com -out server.key

5. 编辑nginx.conf 或者是 default.conf ,增加https代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# HTTPS server
server {
listen 443;
server_name www.mydomain.com;
ssl on;
ssl_certificate /etc/nginx/server.crt;
ssl_certificate_key /etc/nginx/server.key;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;
ssl_protocols SSLv3 TLSv1 TLSv1.1;
ssl_ciphers HIGH:!ADH:!EXPORT56:RC4+RSA:+MEDIUM;
ssl_prefer_server_ciphers on;
location ~ /*login.htm {
proxy_pass http://192.168.1.126:20030;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

这样就OK了,当访问login.htm的时候就可以用https访问了,但是在浏览器里面会出现证书不受信任的问题,可以去第三方机构去购买CA证书。
阿里云上有免费的证书,登录阿里云控制台,点击左侧菜单中的 安全 -> 证书服务,这个页面中右上角有 购买证书 按钮,点击进入购买页,选择免费型DV SSL,购买。然后需要补全域名和个人信息,最后下载证书。
这是阿里云的配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
listen 443;
server_name localhost;
ssl on;
root html;
index index.html index.htm;
ssl_certificate cert/214217202600830.pem;
ssl_certificate_key cert/214217202600830.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}

6. 错误列表

我在配置好Nginx之后,访问地址出现了502错误,查看Nginx错误日志:

1
2
2017/08/08 22:50:54 [crit] 21604#21604: *413 connect() to 192.168.0.106:20050 failed (13: Permission denied) while connecting to upstream, client: 1.202.64.124, server: www.mydomain.com, request: "GET /cd/employee/detail/1 HTTP/1.1", upstream: "http://192.168.0.106:20050/cd/employee/detail/1", host: "www.mydomain.com"
2017/08/08 22:50:55 [error] 21604#21604: *413 open() "/etc/nginx/html/favicon.ico" failed (2: No such file or directory), client: 1.202.64.124, server: www.mydomain.com, request: "GET /favicon.ico HTTP/1.1", host: "www.mydomain.com", referrer: "https://www.mydomain.com/cd/employee/detail/1"

好像是权限问题,试了很多方法,最后执行了一条命令解决了。

setsebool -P httpd_can_network_connect 1 #httpd可以连接到网络

Contents
  1. 1. 1、首先,进入你想创建证书和私钥的目录,例如:
  2. 2. 2、创建服务器私钥,命令会让你输入一个密码,然后重复验证密码:
  3. 3. 3、创建签名请求的证书(CSR),命令依次会你让输入密码、国家、省份、城市、组织名字、主机地址、邮箱,然后是挑战密码和公司名称,这两个不用输入,直接回车:
  4. 4. 4、在加载SSL支持的Nginx并使用上述私钥时除去必须的口令:
  5. 5. 5. 编辑nginx.conf 或者是 default.conf ,增加https代理
  6. 6. 6. 错误列表