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. Python 爬虫入门3种方法

    Python 2.0 url = "http://www.baidu.com" print '第一种方法' response1 = urllib2.urlopen(url) pri ...

  2. React native中的组建通知通信:

    有这么一个需求,在B页面pop()回到A页面,需要A页面执行刷新,那么我们可以采用以下方法: 1:在A页面Push到B页面中,加上一个A页面中的刷新函数做为参数,然后在B页面中在pop()函数封装后通 ...

  3. React入门实例:组件化+react-redux实现网上商城(1)

    项目运行 1.git clone https://github.com/soybeanxiaobi/React_demo_onlineShop 2.cd React_demo_onlineShop(文 ...

  4. “AI”项目日记

    前言:为了更好的以“实践”巩固“学习”,利用空余时间,打造一个属于自己的项目 项目目标: 1.将学习的知识用项目实践,在实践过程中去领悟新的知识 2.高度自由,根据不同时期的学习目标,融入项目中去用代 ...

  5. HAProxy用法详解

    一.HAProxy简介 (1)HAProxy 是一款提供高可用性.负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件,支持虚拟主机,它是免费.快速并且可靠的一种解决方案. HAProx ...

  6. kotlin - 空安全

    空安全设计的操作符号 操作符 作用 ? 可空操作符,声明该值可为空 ?. 安全调用操作符       b?.length 如果b非空,就返回b.length,否则返回 null !! 非空断言运算符, ...

  7. ImageConverter引起的 invalid address or address of corrupt block 0xb7feab58 passed to dlfree

    虹软人脸识别,其方法要传NV21格式的byte[], github上有一个虹软的Demo,是不是虹软工作人员写的不清楚,这个Demo里bitmap转NV21格式byte[]用的是一个第三方库https ...

  8. 学习笔记36—坚果云 | Papership或Zotero使用webDAV验证服务器不成功怎么办?

    很多人都喜欢用坚果云作为Zotero的第三方云盘,从而无限扩展Zotero的存储空间.可是大家在Papership或zotero客户端中验证坚果云webDAV服务器时,会出现验证不成功的问题,相信这个 ...

  9. d3 .each()

    d3.select("xxx") .each(function (d) { //this表示当前element 而 d表示绑定的数据 something(this); }); 注意 ...

  10. docker下debian镜像开启ssh, 允许root用密码登录

    用的官方python镜像做开发, 暴露端口, 用pycharm ssh进去开发. 忽然发现本来ssh能连上, 但是更了新的python镜像连不上了. 有折腾了一下, 连上了. 主要是python官网镜 ...