应用场景:
内容重定向到另外一个url, 但是链接保持不变。需要提前安装openresty。
实现方法
1. 打开nginx文件夹(以下路径为默认路径)
cd /usr/local/openresty/nginx/conf
2.查看nginx.conf文件,是否有server块
vi nginx.conf
nginx.conf中没有server块,但是有
include vhosts/*.conf;
说明server块写在vhosts文件夹下,打开文件夹,并且查看列表
cd vhosts
ls 或 ll
新建xxx.xxx.xxx.conf,权限 -rw-r–r–,即644,默认不用改
vim xxx.xxx.xxx.conf
方便复制版:
server {
listen 443 ssl;
server_name xxx.xxx.xxx;
root /路径/路径/xxx.xxx.xxx;
index index.html;
ssl_certificate cert/xxx.xxx.xxx.pem;
ssl_certificate_key cert/xxx.xxx.xxx.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;
include error.conf;
location / {
proxy_pass http://127.0.0.1:10001/;
}
}
server {
listen 80;
server_name xxx.xxx.xxx;
root /路径/路径/xxx.xxx.xxx;
index index.html;
}
注释版:
server {
#监听443端口(https默认端口)
listen 443 ssl;
server_name xxx.xxx.xxx;
#打开xxx.xxx.xxx访问的html
root /路径/路径/xxx.xxx.xxx;
index index.html;
#ssl 密钥(申请ssl证书拿到)
ssl_certificate cert/xxx.xxx.xxx.pem;
ssl_certificate_key cert/xxx.xxx.xxx.key;
#timeout 超时
ssl_session_timeout 5m;
#配置 HTTPS 服务器所支持的 SSL/TLS 密码套件,以确保服务器只支持安全的密码套件,并且拒绝不安全的密码套件
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
#配置 HTTPS 服务器所支持的 SSL/TLS 协议,以确保服务器只支持安全的协议,并且拒绝不安全的协议
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#配置 HTTPS 服务器是否优先使用服务器支持的密码套件
#默认是off, 开启后会优先使用服务器支持的密码套件,而不是客户端支持的密码套件
#配置这个选项,可以减少密码套件的交互,从而提高安全性
ssl_prefer_server_ciphers on;
#访问错误的配置,没有可忽略,在nginx/conf里
include error.conf;
#反向代理定向到另一个url,/为输入网址默认访问的目录,也可以使用/example等,网址体现为https://xxx.xxx.xxx/example
location / {
proxy_pass http://127.0.0.1:10001/;
}
}
server {
#监听80端口(http默认端口)
listen 80;
server_name xxx.xxx.xxx;
#打开xxx.xxx.xxx访问的html
root /路径/路径/xxx.xxx.xxx;
index index.html;
}
链接保持不变重定向到另外一个url(反向代理):
location [ = | ~ | ~* | ^~] uri {
#URL为被代理服务器的地址,可以包含传输协议、主机名称或IP地址加端口号,URI等。
proxy_pass URL;
}
#如:
location / {
proxy_pass http://127.0.0.1:10001/;
}
1、= :用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配成功,就停止继续向下搜索并立即处理该请求。
2、~:用于表示 uri 包含正则表达式,并且区分大小写。
3、~*:用于表示 uri 包含正则表达式,并且不区分大小写。
4、^~:用于不含正则表达式的 uri 前,要求 Nginx 服务器找到标识 uri 和请求字符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location 块中的正则 uri 和请求字符串做匹配。
注意:如果 uri 包含正则表达式,则必须要有 ~ 或者 ~* 标识。
3. 重启nginx
# 检测nginx能否运行
/usr/local/openresty/nginx/sbin/nginx -t
# 重启nginx
service nginx restart
配置ssl密钥
1.打开cert文件夹
cd /usr/local/openresty/nginx/conf/cert
2.上传密钥文件(.key和.pem)
scp 本地地址/xxx.key root@服务器地址:/usr/local/openresty/nginx/conf/cert/xxx.key
scp 本地地址/xxx.pem root@服务器地址:/usr/local/openresty/nginx/conf/cert/xxx.pem
3.更改权限为-rwxr-xr-x(755)
chmod 755 xxx.key
chmod 755 xxx.pem
参考链接:https://blog.csdn.net/zxd1435513775/article/details/102508463