Nginx学习笔记(三)

一、Rewrite用法

1、语法:

rewrite regex replacement [flag];

2、功能:
使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向。rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用,例如 http://seanlook.com/a/we/index.php?id=1&u=str 只对/a/we/index.php重写

3、flag标志位

last : 相当于Apache的[L]标记,表示完成rewrite
break : 停止执行当前虚拟主机的后续rewrite指令集
redirect : 返回302临时重定向,地址栏会显示跳转后的地址
permanent : 返回301永久重定向,地址栏会显示跳转后的地址
因为301和302不能简单的只返回状态码,还必须有重定向的URL,这就是return指令无法返回301,302的原因了。这里 last 和 break 区别有点难以理解:

last一般写在server和if中,而break一般使用在location中
last不终止重写后的url匹配,即新的url会再从server走一遍匹配流程,而break终止重写后的匹配
break和last都能组织继续执行后面的rewrite指令

分析last和break的不同,举个例子

location /test1.txt {
  root rootPath   rewrite
/test1.txt/ /test2.txt break; } location ~ test2.txt { return 404; }

上面例子如果访问http://***.com/test1.txt,会访问rootPath下面的test2.txt输出,如果break改为last,同样的请求,在rewrite之后会重新走一遍server,最终返回404

二、设置php-fpm socket连接

1、修改php-fpm.conf配置

listen = /tmp/php-fpm.sock

2、修改nginx.conf配置

fastcgi_pass  unix:/tmp/php-fpm.sock; 

原文地址:https://www.cnblogs.com/hifelix/p/5909994.html