网站首页 > 技术教程 正文
nginx反向代理加https证书和自动跳转配置
如果对运维课程感兴趣,可以在b站上搜索我的账号: 运维实战课程,可以关注我,学习更多免费的运维实战技术视频
1.机器规划:
nginx代理机器:192.168.14.128
tomcat1机器: 192.168.14.129
tomcat2机器:192.168.14.130
2.实际操作步骤
1)不用nginx代理时候的直接访问后端网站:
2)配置域名解析(即类似DNS解析,通过域名能解析到nginx机器的IP,此处在本地window机器的hosts文件中配置解析,也可由自己的DNS服务器解决)
aaaaa.hotread.com <-------> 192.168.14.128 (相互对应)
3)nginx机器上(192.168.14.128)安装nginx,并配置反向代理,能通过nginx负载均衡访问服务
[root@bogon ~]# useradd nginx
[root@bogon ~]# yum -y install gcc gcc-c++
[root@bogon ~]# yum -y install openssl-devel zlib-devel pcre-devel
[root@bogon ~]# ls
nginx-1.0.5.tar.gz
[root@bogon ~]# tar -zxf nginx-1.0.5.tar.gz
[root@bogon ~]# ls
nginx-1.0.5 nginx-1.0.5.tar.gz
[root@bogon ~]# cd nginx-1.0.5
[root@bogon nginx-1.0.5]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@bogon nginx-1.0.5]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_ssl_module
[root@bogon nginx-1.0.5]# make && make install
[root@bogon nginx-1.0.5]# ls /usr/local/nginx/
conf html logs sbin
[root@bogon nginx-1.0.5]# vim /usr/local/nginx/conf/nginx.conf
.........
upstream myserver {
server 192.168.14.129:8080;
server 192.168.14.130:8080;
}
server {
listen 80;
server_name aaaaa.hotread.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://myserver;
}
#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;
}
}
[root@bogon nginx-1.0.5]# /usr/local/nginx/sbin/nginx
[root@bogon nginx-1.0.5]# netstat -anput |grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3691/nginx
浏览器访问:http://aaaaa.hotread.com/
4)在阿里云上申请域名对应的ca证书和私钥(aaaaa.hotread.com),然后下载下来
下载后,解压并重命名后my.key和my.pem,如下图:
5)在nginx机器上配置https证书并配置能强制跳转到https的访问
[root@bogon nginx-1.0.5]# vim /usr/local/nginx/conf/nginx.conf
....................
upstream myserver {
server 192.168.14.129:8080;
server 192.168.14.130:8080;
}
#注意:下面已测:只有301时候可以跳转,307或其他不能跳转,或者不用if判断,直接使用跳转那条也可。
server {
listen 80;
server_name aaaaa.hotread.com;
if ($scheme = http){
return 301 https://$host$request_uri;#或return 301 https://$server_name$request_uri;
}
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://myserver;
}
#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;
}
}
server {
listen 443;
server_name aaaaa.hotread.com;
ssl on;
ssl_certificate /usr/local/nginx/ssl/my.pem;
ssl_certificate_key /usr/local/nginx/ssl/my.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
proxy_pass http://myserver;
}
}
[root@bogon nginx-1.0.5]# mkdir /usr/local/nginx/ssl
[root@bogon nginx-1.0.5]# cd /usr/local/nginx/ssl/
[root@bogon ssl]# rz
上传上面改过名的证书和私钥,如下:
[root@bogon ssl]# ls
my.key my.pem
[root@bogon ssl]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@bogon ssl]# /usr/local/nginx/sbin/nginx -s reload
客户端浏览器访问时候能自动跳转,如下:
访问http://aaaaa.hotread.com 会自动跳转到 https://aaaaa.hotread.com 且没有不安全的提示
回车后,如下:刷新还可以轮询
如果对运维课程感兴趣,可以在b站上搜索我的账号: 运维实战课程,可以关注我,学习更多免费的运维实战技术视频
猜你喜欢
- 2024-10-09 不懂Nginx正反向代理?没关系,只要会买东西你就明白了
- 2024-10-09 深度详解Nginx正向代理与反向代理?
- 2024-09-10 如何玩转nginx正反向代理(nginx反向代理的几种模式)
- 2024-09-10 nginx如何配置正向代理之非常简单操作
- 2024-09-10 nginx常用模块及反向代理(nginx反向代理配置实例)
- 2024-09-10 nginx代理https妈妈级教程(nginx代理https)
- 2024-09-10 图文讲解:如何使用 Nginx 反向代理、负载均衡
- 2024-09-10 nginx配置tcp代理(nginx tcp代理)
- 2024-09-10 使用 Nginx 作为你的开发代理工具
- 2024-09-10 网页服务-Nginx - 反向代理 - 基于浏览器
你 发表评论:
欢迎- 最近发表
-
- linux日志文件的管理、备份及日志服务器的搭建
- Linux下挂载windows的共享目录操作方法
- Linux系统中的备份文件命令(linux系统中的备份文件命令有哪些)
- 麒麟KYLINOS|通过不同方法设置用户访问文件及目录权限
- 「Linux笔记」系统目录结构(linux目录的结构及含义)
- linux中修改归属权chown命令和chgrp命令
- 工作日报 2021.10.27 Android-SEAndroid权限问题指南
- Windows和Linux环境下,修改Ollama的模型默认保存路径
- 如何强制用户在 Linux 上下次登录时更改密码?
- 如何删除Linux文件夹中除某些扩展名之外的所有文件?
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)