nginx 通过 location 的规则匹配将 php 转发给 php-fpm 处理后获取结果然后返回给客户端,转发模式可以通过 unix sock 或 tcp socket 方式。百度了好多文章我是没遇到一个能完整的而且正确的把 nginx 和 php 结合的配置讲述的较为正确的,这里总结了下最基本的 nginx + php 的模式配置,以及隐藏 index.php 和 开启 pathinfo 模式的方法。

个人觉得是可以复制粘贴配置你的生产环境的,总结了很多好的博文的要点,比如隐藏 index.php 的 location 规则用的是 try_files 而不是烂大街的 if (! -e $uri) {},http 服务器级配置文件 和 虚拟主机配置文件也很好的分割开了,方便维护。

nginx 配置分两大层,基础的全局 http 配置 和 与主机相对应的 server 配置。

http 配置

nginx.conf

# nginx main configure

user  www www;

worker_processes auto;

error_log  /var/log/error.log  crit;

pid        /var/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 2048; events
{
use epoll;
worker_connections 2048;
multi_accept on;
} http
{
include mime.types;
default_type application/octet-stream; server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m; sendfile on;
tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k; gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\."; #limit_conn_zone $binary_remote_addr zone=perip:10m;
##If enable limit_conn_zone,add "limit_conn perip 10;" to server section. server_tokens off;
access_log off; # http 配置
include vhost/*.conf;
}

nginx  http 的主配置文件,这里面包含了用户组,日志,处理类型,压缩传输,并发数等参数配置。我们并没有在这里配置 server,而是将所有的 server 放置到 vhost 文件中,清晰的管理我们的 server 虚拟主机配置。我们可以将不同 server 服务器单独配置为 conf 文件。

server 配置

比如我们配置一虚拟主机 default

vhost/default.conf

这里面参数配置包括:隐藏 index.php,开启php处理或开启php pathinfo模式,单独处理静态资源

注意:

如果你想开启 pathinfo 模式只需要将 enable-php.conf 改为 enable-php-pathinfo.conf 即可,二者选其1

server
{
listen 80;
#listen [::]:80;
server_name www.default.com;
index index.html index.htm index.php;
root /var/www/default; #error_page 404 /404.html; #hide index.php
location / {
# yii2 框架的 /site/index?name=sallency&age=25 模式的 rewrite 方法
try_files $uri $uri/ /index.php$is_args$args;
# tp 框架的 /site/index/name/sallency/age/25 模式的 rewrite 方法
try_files $uri $uri/ /index.php/$uri;
# 虽然 if 不规范但 rewrite 还是很方便的 可以兼容 yii2 和 tp 的 pathinfo 模式
if (!-e $request_filename){
rewrite ^/(.*)$ /index.php/$1 last;
break;
}
} #handler php request
include enable-php.conf; #php with pathinfo
#include enable-php-pathinfo.conf; #handler static resource
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
} location ~ .*\.(js|css)?$
{
expires 12h;
} #forbidden access . type
location ~ /\.
{
deny all;
} access_log /var/log/nginx/default_access.log
}

配置 php

enable-php.conf

开启此配置便可以让 nginx 处理 php 文件,需要注意的是  fastcig_pass 的模式有两种:

unix socket:不走网卡 效率高但不稳定

tcp socket:127.0.0.1:9000 相比 unix socket 会慢一点点,但稳定性高出很多

此处的模式和配置选择应与 php-fpm.conf 中的 listen 参数保持一致:

listen = /tmp/php-cgi.sock

listen = 127.0.0.1:9000

 location ~ [^/]\.php(/|$)
{
try_files $uri =404; #listen unix socket
#fastcgi_pass unix:/tmp/php-cgi.sock;
#listen tcp socket
fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php;
include fastcgi.conf;
}

配置 pathinfo 模式

enable-php-pathinfo.conf

此配置文件为 enable-php.conf 的增强版-- 开启 pathinfo 模式,流行的 php 框架都支持此模式

  location ~ [^/]\.php(/|$)
{
#listen unix socket
#fastcgi_pass unix:/tmp/php-cgi.sock;
#listen tcp socket
fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php;
include fastcgi.conf; #pathinfo
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
try_files $fastcgi_script_name =404;
}

fastcgi.conf

这个配置文件其实是 nginx 自带的,我贴一下

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty; fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name; # PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;

配置完成,注意因为所有的配置文件都是由 nginx.conf 这个主配置文件作为入口进行加载的,所以活动目录始终是在 nginx.conf 所在的目录,所以配置文件中的 include 的当前路径是 nginx.conf 所在的目录,配置完成后重启 nginx service 的同时记得重启 php-fpm 服务

nginx 隐藏 index.php 和 开启 pathinfo 模式的配置的更多相关文章

  1. nginx 隐藏index.php 并开启rewrite日志调试(apache也有)

    开启rewrite 日志 error_log       /data/log/nginx/error.log notice; 位于最外层,大约在文件的前几行 再在http{}括号里增加一行:rewri ...

  2. nginx 下开启pathinfo模式

    前几天自己新弄了个服务器,nginx的环境, 看到thinkcmf的框架,下载下来准备研究下,安装完成后,发现 url 是 普通模式,然后我就按照那个手册去后台开启了pathinfo模式,这一改完蛋了 ...

  3. Nginx隐藏index.php和配置vhost

    nginx启动命令 启动:nginx停止:nginx -s stop退出:nginx -s quit重启:nginx -s reopen重新加载:nginx -s reload平滑启动:kill -H ...

  4. nginx下开启pathinfo模式

    第一种方式是通过重写url来实现pathinfo模式: location / { if (!-e $request_filename){ rewrite ^/(.*)$ /index.php?s=/$ ...

  5. php开启pathinfo 模式

    pathinfo 模式 需要 php.ini 开启下面这个参数 cgi.fix_pathinfo=1 path_info模式:http://www.xxx.com/index.php/模块/方法   ...

  6. nginx 隐藏 index.php

    使用情景如下: 在访问 http://php.cc/Att/AttList 的时候.跳转到 http://php.cc/index.php/Att/AttList : 也就是开启重写功能: 在ngin ...

  7. centos7 nginx完整支持thinkphp pathinfo模式开启方法

    thinkphp运行在linux+nginx,如何开启pathinfo模式,我是完整测试过了,没问题的,在thinkphp配置文件 开启    'URL_MODEL'   =>  1,   1代 ...

  8. 关于开启url的pathinfo模式

    1.apache要开启pathinfo模式,需要在 <Directory /> Options +Indexes +FollowSymLinks +ExecCGI AllowOverrid ...

  9. nginx支持pathinfo模式

    很久不使用apache了,渐渐对apache感到陌生,因为朋友有个ZendFramework框架从apache移到nginx下,需要pathinfo模式支持.网上海搜于是开始搜索nginx+pathi ...

随机推荐

  1. Type mismatch: cannot convert from javax.servlet.http.Cookie[] to org.apache.tomcat.util.http.parser.Cookie[] 的一种可能

    今天用到Cookie时,写了一个Cookie数组,发现报错“Type mismatch: cannot convert from javax.servlet.http.Cookie[] to org. ...

  2. vue中eslintrc.js配置最详细介绍

    本文是对vue项目中自带文件eslintrc.js的内容解析, 介绍了各个eslint配置项的作用,以及为什么这样设置. 比较详细,看完能对eslint有较为全面的了解,基本解除对该文件的疑惑. /* ...

  3. unbuntu安装Node.js

    在官网https://nodejs.org/en/下载 手动创建链接的话,新安装的angular的ng   typescript的tsc都要自己手动建立软链接,要不就每个工程里npm install一 ...

  4. (转)Lua的table库函数insert、remove、concat、sort详细介绍

    原帖链接:http://www.jb51.net/article/64711.htm#comments 有增注标识的地方为额外注释,非原帖内容. 函数列表:(增注:只能用于数组!) table.ins ...

  5. 自动化部署之jenkins及简介

    一.什么是持续集成? (1)Continuous integration(CI) 持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通常每个成员至少集成一次,也就意味着每天可能会发生多次集 ...

  6. spring cloud: Hystrix(八):turbine集群监控(dashboard)

    turbine是聚合服务器发送事件流数据的一个工具,hystrix的监控中,只能监控单个节点,实际生产中都为集群, 因此可以通过turbine来监控集群下hystrix的metrics情况,通过eur ...

  7. 如何跳过开机密码进入windows系统

    工具:安装好PE系统的u盘一个. 开机时按F12(每个电脑不同)进入PE系统,将c盘目录下的SAM文件拷入自己的u盘内.SAM文件的位置:  C:\WINDOWS\system32\config\SA ...

  8. 扩大了一个逻辑卷,resize2fs 保错:没有这个超级块

    检查发现,文件系统类型是xfs,应该使用 xfs_growfs命令刷新文件系统

  9. python 中的 and / or

    逻辑运算符:and   or    not 优先级:       not > and > or 数字:0为假, 非0为真: 字符串:空为假,非空为真: 逻辑表达式的值: x   and   ...

  10. Lombok插件

    Lombok简介 Lombok是一款好用顺手的工具,就像Google Guava一样,在此予以强烈推荐,每一个Java工程师都应该使用它.Lombok是一种Java™实用工具,可用来帮助开发人员消除J ...