码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx的rewrite模块疑问排查

时间:2017-10-28 11:05:39      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:nginx rewrite

标题索引


  • 追溯原因

  • 过程分析

  • 原理总结


追踪原因

    最近心态"一步一印,有印为证",在Nginx的rewrite模块在工作过程中,客户端发起包到服务器解包整体过程浏览器做了什么?服务器做了什么?到底是服务器端接收请求匹配跳转条件后,先执行跳转并将执行结果反馈给客户端呢?还是服务器端接收到请求,先反馈给客户端跳转后的路径,客户端再次重新发起请求,服务器端再次接收请求并执行请求,最后将执行结果反馈给客户端?为此决定抓包分析一探究竟。

过程分析

    为验证此

    当服务器配置rewrite参数为永久性重定向时,实验如下:

[ root@nginxagent conf.d ]#pwd
/etc/nginx/conf.d
[ root@nginxagent conf.d ]mkdir /app/website01/jn
[ root@nginxagent conf.d ]mkdir /app/website01/jncsy
[ root@nginxagent conf.d ]echo jn >/app/website01/jn
[ root@nginxagent conf.d ]echo jncshy >/app/website01/jncsy
[ root@nginxagent 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时,实验如下:

[ root@nginxagent 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,实验如下:

[ root@nginxagent ~ ]#mkdir /etc/nginx/ssl
#----------------------------------生成自签名秘钥和证书------------
[ root@nginxagent ~ ]#cd /etc/pki/tls/certs
[ root@nginxser01 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 []:
#-------------------------验证生成的证书和秘钥------------------------------
[ root@nginxser01 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
#-------------------------避免每次调用私钥进行输入密码验证进行解密-----------
[ root@nginxser01 certs ]#openssl rsa -in nginx.key -out nginx.key
Enter pass phrase for nginx.key:
writing RSA key
[ root@nginxser01 certs ]#cp ngi* /etc/nginx/ssl
[ root@nginxagent 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模块疑问排查

标签:nginx rewrite

原文地址:http://weiboxue.blog.51cto.com/6506379/1976905

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!