Nginx的rewrite模块疑问排查
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx的rewrite模块疑问排查相关的知识,希望对你有一定的参考价值。
标题索引
追溯原因
过程分析
原理总结
追踪原因
最近心态"一步一印,有印为证",在nginx的rewrite模块在工作过程中,客户端发起包到服务器解包整体过程浏览器做了什么?服务器做了什么?到底是服务器端接收请求匹配跳转条件后,先执行跳转并将执行结果反馈给客户端呢?还是服务器端接收到请求,先反馈给客户端跳转后的路径,客户端再次重新发起请求,服务器端再次接收请求并执行请求,最后将执行结果反馈给客户端?为此决定抓包分析一探究竟。
过程分析
为验证此
当服务器配置rewrite参数为永久性重定向时,实验如下:
[ [email protected] conf.d ]#pwd /etc/nginx/conf.d [ [email protected] conf.d ]mkdir /app/website01/jn [ [email protected] conf.d ]mkdir /app/website01/jncsy [ [email protected] conf.d ]echo jn >/app/website01/jn [ [email protected] conf.d ]echo jncshy >/app/website01/jncsy [ [email protected] conf.d ]#vim virtual.conf #server test server { listen 80; index index.html; server_name root /app/website01/; location /zz { rewrite ^/jn/(.*)$ /jncsy/$1 permanent; } }
当服务器配置rewrite参数为last时,实验如下:
[ [email protected] conf.d ]#vim virtual.conf #server test server { listen 80; index index.html; server_name www.a.com; root /app/website01/; location /zz { rewrite ^/jn/(.*)$ /jncsy/$1 last; } }
当服务器配置https,由http跳转至https时,且重定向为redirect,实验如下:
[ [email protected] ~ ]#mkdir /etc/nginx/ssl #----------------------------------生成自签名秘钥和证书------------ [ [email protected] ~ ]#cd /etc/pki/tls/certs [ [email protected] certs ]#make nginx.crt umask 77 ; /usr/bin/openssl genrsa -aes128 2048 > nginx.key Generating RSA private key, 2048 bit long modulus .........+++ .........................................+++ e is 65537 (0x10001) Enter pass phrase: #创建私钥,并输入密码# Verifying - Enter pass phrase: #确认密码# umask 77 ; /usr/bin/openssl req -utf8 -new -key nginx.key -x509 -days 365 -out nginx.crt -set_serial 0 Enter pass phrase for nginx.key: #利用私钥生成证书,输入私钥密码# You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter ‘.‘, the field will be left blank. ----- Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:SHANXI Locality Name (eg, city) [Default City]:XI‘AN Organization Name (eg, company) [Default Company Ltd]:JNCSY Organizational Unit Name (eg, section) []:OPT Common Name (eg, your name or your server‘s hostname) []:www.a.com Email Address []: #-------------------------验证生成的证书和秘钥------------------------------ [ [email protected] certs ]#ll ngi* -rw------- 1 root root 1289 Oct 28 06:47 nginx.crt -rw------- 1 root root 1766 Oct 28 06:43 nginx.key #-------------------------避免每次调用私钥进行输入密码验证进行解密----------- [ [email protected] certs ]#openssl rsa -in nginx.key -out nginx.key Enter pass phrase for nginx.key: writing RSA key [ [email protected] certs ]#cp ngi* /etc/nginx/ssl [ [email protected] conf.d ]#vim virtual.conf #server test server { listen 80; index index.html; server_name www.a.com; root /app/website01/; location / { rewrite ^/(.*)$ https://172.18.27.22/$1 redirect; } } server { listen 443 ssl; server_name www.a.com; index index.html; root /app/website01/; ssl on; ssl_certificate /etc/nginx/ssl/nginx.crt; ssl_certificate_key /etc/nginx/ssl/nginx.key; ssl_session_cache builtin:1000 shared:SSL:20m; ssl_session_timeout 10m; }
当服务器配置https,由http跳转至https时,且重定向为permanent,实验如下:
sed
总结对比
根据本次实验,总结得知,若rewrite ^/zz(.*)$ /zhengzhou/$1 last;服务器端直接进行,http https则先返客户端,再次重新发起新的请求。
本文出自 “一步一印,有印为证” 博客,谢绝转载!
以上是关于Nginx的rewrite模块疑问排查的主要内容,如果未能解决你的问题,请参考以下文章