nginx 配置 php url 重写
nginx 配置 Https php url 重写
server {
listen 80;
server_name www.goodsunlc.com;
rewrite ^(.*) https://$server_name$1 permanent;
}
server {
listen 443 ssl;
ssl on;
server_name www.goodsunlc.com;
ssl_certificate /etc/letsencrypt/live/www.goodsunlc.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.goodsunlc.com/privkey.pem;
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;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
access_log /home/wwwlogs/www.goodsunlc.com.access.log;
error_log /home/wwwlogs/www.goodsunlc.com.error.log;
location ~* /status/(\w+)/(.*) {
alias /home/work/activity/$1/web/$2;
break;
}
rewrite ^/(site.xml|robots.txt|work)/(.*)$ /$1/$2 break;
#\.php 只处理动态请求,对于静态资源请求由下面的 location匹配和处理
location ~ \.php {
root /home/work/blog/web;
fastcgi_pass unix:/tmp/php-cgi5.6.sock;
#包含nginx服务器传递给fastcgi程序的参数,php中通过$_SERVER['参数名']可获取
include fastcgi.conf;
#定义变量$fastcgi_script_name_new赋值为$fastcgi_script_name变量
set $path_info "";
set $fastcgi_script_name_new $fastcgi_script_name;
#判断url是否是pathinfo形式的,如果是则把这个url分割成两部分,index.php入口文件之后的pathinfo部分存入$path_info变量中,剩下的部分和$document_root根目录定位index.php入口文件在文件系统中的绝对路径 .
if ($fastcgi_script_name ~* "^(.+\.php)(/.+)$" ) {
set $fastcgi_script_name_new $1;
set $path_info $2;
}
#对fastcgi.conf中的SCRIPT_FILENAME和SCRIPT_NAME fastcgi参数进行重写,目的是指定入口文件在文件系统中的绝对路径给script_filename参数,让fastcgi知道index.php文件位置。
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name_new;
fastcgi_param SCRIPT_NAME $fastcgi_script_name_new;
#定义一个新的nginx服务器传递给fastcgi的参数PATH_INFO,thinkphp需要这个入口文件index.php后的pathinfo信息
fastcgi_param PATH_INFO $path_info;
}
# 用来匹配静态资源,如果不是静态资源就重写,然后重新轮训所有的location块,由上面的location块匹配后动态处理这个请求
location / {
root /home/work/blog/web;
index index.php index.html index.htm;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php$1 last;
}
}
}