Nginx配置HTTPS详细教程

Nginx配置HTTPS详细教程

前置条件:在配置https之前请确保下面的步骤已经完成

  • 服务器已经安装nginx并且通过http可以正常访问
  • 拥有ssl证书,没有的可以去阿里购买或者免费申请一年 ,如果以上条件都满足了,接下来开始配置https

第一步:Nginx的ssl模块安装

在配置ssl证书之前,要确保你的nginx已经安装了ssl模块,一般情况下自己安装的nginx都是不会存在ssl模块的
这里先检查下自己是否存在ssl模块:
进入到你的nginx安装目录下面,我的目录是在(/usr/local/nginx),如果你的nginx安装步骤和上面的文章一致的话,那你的目录和我应该是一致的
进入到目录的sbin目录下,输入:

1
2
# 注意这里是大写的V,小写的只显示版本号
nginx -V

如果出现 (configure arguments: --with-http_ssl_module), 则已安装(下面的步骤可以跳过,直接进行ssl配置步骤)

一般情况下都是不存在ssl模块的,接下来进入到你的解压缩后的nginx目录,注意这里不是nginx安装目录,是解压缩后的目录,我的是在(/usr/local/src/nginx),进入目录后,输入:

1
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

接下来执行:

1
2
make
# 切记不要执行make install,否则会重新安装nginx

上述操作执行完成以后,你的目录下会出现objs文件夹,文件夹内存在nginx文件

接下来使用新的nginx文件替换掉之前安装目录sbin下的nginx,注意这里的替换的时候可以先将之前的文件备份下,停掉nginx服务

1
2
3
4
nginx -s stop # 停止nginx服务

# 替换之前的nginx
cp /usr/local/src/nginx/objs/nginx /usr/local/nginx/sbin

成功之后,进入到nginx安装目录下,查看ssl信息是否编译成功

1
2
3
# 注意这里是大写的V,小写的只显示版本号
nginx -V
# 可以看到这里出现了configure arguments: --with-http_ssl_module证明已经安装成功

提示:这里替换后在执行 -V命令如果提示权限不足,先给这个nginx文件提升下权限

第二步:配置ssl证书

解压缩下载好的证书(证书一般是pem文件和key文件,这里名字可以随便改) 将下载好的证书上传到服务器,我将证书放在了/data/web/目录下的ssl文件夹

1
2
# 创建ssl文件夹
mkdir -p /data/web/ssl

第三步:进行nginx.conf配置

进入nginx.conf文件下

1
2
3
cd /usr/locla/nginx/conf
# 修改nginx.conf文件
vim nginx.conf

打开之后文件内容如图:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
user  www;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}
include servers/*;
}

可以将没用的东西都删除掉,删除的时候注意,括号要对应起来

进入servers/website.conf然后进行配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
server {
listen 443 ssl;
server_name hqd8080.me;
ssl on;
ssl_certificate /data/web/ssl/hqd8080.me.pem;
ssl_certificate_key /data/web/ssl/hqd8080.me.key;

ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

root /data/web/public;

access_log /data/web/logs/hqd8080.me.access.log;
error_log /data/web/logs/hqd8080.me.error.log;

client_max_body_size 10m;

location / {
index index.html index.htm;
}

location ~ ^/(uploads|assets)/.*\.(php|php5|jsp)$ {
deny all;
}

# 配置后台服务api接口服务 代理到8080端口
location ~ ^/dev/ {
proxy_set_header Host $http_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;
rewrite ^/dev/(.*)$ /$1 break;
proxy_pass http://127.0.0.1:8080;
}

error_page 404 /404.html;
error_page 403 /403.html;
}

server {
listen 80;
server_name hqd8080.me;
#将请求转成https
rewrite ^(.*)$ https://$host$1 permanent;
}

注意:这里需要在安全组中开放443端口

第四步:重启nginx

ok,如果上述步骤都完成了,没有问题,接下来只需要重启nginx服务即可

进入sbin目录下,输入:

1
2
3
./nginx -s reload
./nginx -s stop
./nginx

完成