nginx安装ssl模块
查看nginx安装的模块
停止nginx服务
复制代码
1./nginx -s stop
进入nginx解压目录,重新编译nginx
复制代码
1
2
3
4cd /usr/local/nginx/nginx-1.17.6 // --prefix指定nginx的安装目录 ./configure --prefix=/usr/local/nginx/nginx-config --with-http_stub_status_module --with-http_ssl_module
编译完成之后在当前目录继续执行make命令(Makefile文件的目录)
复制代码
1make
之后,将当前目录下的objs目录中的nginx程序,拷贝到nginx的安装目录/usr/local/nginx/nginx-config/sbin目录下
复制代码
1cp objs/nginx /usr/local/nginx/nginx-config/sbin/nginx
进入nginx安装目录,查看nginx安装的模块
复制代码
1
2
3cd /usr/local/nginx/nginx-config/sbin/ ./nginx -V
nginx配置https
证书申请(此处省略1w步)成功后,将证书下载到本地
将下载的证书解压,解压后得到 ***.pem和***.key 两个文件,将这两个文件上传到linux目录(nginx安装目录:/usr/local/nginx/nginx-config/conf/cert)
配置nginx.conf文件,在http模块下添加server模块
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22server { listen 443 ssl; #SSL协议访问端口号为443。此处如未添加ssl,可能会造成Nginx无法启动。 server_name api-*******.com; #将localhost修改为您证书绑定的域名,例如:www.example.com。 root html; index index.html index.htm; ssl_certificate cert/4316713_api-******.com.pem; #将domain name.pem替换成您证书的文件名。 ssl_certificate_key cert/4316713_api-******.com.key; #将domain name.key替换成您证书的密钥文件名。 ssl_session_timeout 10m; 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; #站点目录。使用代理的话这行替换成 proxy_pass http://127.0.0.1:8080; index index.html index.htm; } } }
开放443端口(只开放安全组端口是不行的)
复制代码
1
2
3
4
5
6
7
8
9
10// centos7 命令 // 添加 firewall-cmd --zone=public --add-port=443/tcp --permanent // 重新载入 firewall-cmd --reload // 查看是否开放成功,成功返回yes firewall-cmd --zone=public --query-port=443/tcp
开放安全组443端口
重启nginx服务
复制代码
1
2
3cd /usr/local/nginx/nginx-config/sbin/ ./nginx -s reload
浏览器中使用https访问,看下配置效果
下面出一个 https 访问前端,前端 https 访问后端
复制代码
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# 前端 https 访问配置 server { listen 443 ssl; # 浏览器要访问前端 nginx 要访问的端口,例如浏览器访问:https://aa.com:9000/login , 这里就写 9000 server_name aa.com; # 当前服务器映射的域名 root html; index index.html index.html; ssl_certificate cert/6680189_aa.com.pem; # ssl证书的路径 ssl_certificate_key cert/6680189_aa.com.key; # ssl证书的路径 ssl_session_timeout 10m; 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 /opt/jar/vue/; # 前端文件存放在linux服务器的目录 index index.html; try_files $uri $uri/ /index.html; # 这个是解决页面一刷新就跳404 } location /server_file { # (这个可以删除)这个是配置文件上传的,例如你的文件访问地址是:https://aa.com:9000/server_file/a.jpg alias /opt/jar/server_file/; # 文件存放在linux服务器中的目录,server_file 要和上一行保持一致,因为使用的是 alias用户 index index.html; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } error_page 497 https://$host:$server_port$uri$is_args$args; } # HTTPS server # # 后端 https 访问配置 server { listen 9777 ssl; # 后端 nginx 代理的后端端口,例如后端通过https访问是这样的:https://aa.com:9777 server_name aa.com; # 当前服务器映射的域名 root html; index index.html index.html; ssl_certificate cert/6680189_aa.com.pem; ssl_certificate_key cert/6680189_aa.com.key; ssl_session_timeout 10m; 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 / { proxy_pass http://127.0.0.1:9700; # 后端项目的地址,比如你的java程序的端口是9700,可以通过 http://ip:9700访问,也可以通过nginx代理的 9777端口通过域名访问:https://aa.com:9777 index index.html index.htm; } } # 访问 80 跳转到 443 配置 server { listen 80; server_name aa.com; return 301 https://$host$request_uri; }
nginx配置ws
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15# 配置 ws, 通过 ws://ip:9909/ws 访问 server { listen 9909; # nginx代理的端口,webSocket 可以通过这样访问: ws://IP:9909/路径 server_name localhost; location / { proxy_pass http://127.0.0.1:9099/; # netty服务的端口 9099,本地通过这样访问: ws://127.0.0.1:9099/路径 proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_connect_timeout 300s; proxy_read_timeout 300s; proxy_send_timeout 300s; } }
端口后面的/ws,是你项目中的路径
nginx配置wss
复制代码
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# 配置 wss 访问,wss://aa.com:9909/ws server { listen 9909 ssl; server_name aa.com; root html; index index.html index.html; ssl_certificate cert/6680189_aa.com.pem; ssl_certificate_key cert/6680189_aa.com.key; ssl_session_timeout 10m; 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 / { proxy_pass http://127.0.0.1:9099/; # netty服务的端口 9099,本地通过这样访问: ws://127.0.0.1:9099/路径 proxy_http_version 1.1; 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-Nginx-Proxy true; proxy_redirect off; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_connect_timeout 300s; proxy_read_timeout 300s; proxy_send_timeout 300s; } }
最后
以上就是可耐棉花糖最近收集整理的关于nginx添加http_ssl_module模块、nginx配置https、nginx配置ws、nginx配置wss的全部内容,更多相关nginx添加http_ssl_module模块、nginx配置https、nginx配置ws、nginx配置wss内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复