您的位置 首页 技术

nginx隐藏index.php和开启pathinfo模式的配置方法详解

nginx 通过 location 的规则匹配将 php 转发给 php-fpm 处理后获取结果然后返回给客户端,转发模式可以通过 unix sock 或 tcp socket 方…

nginx 通过 location 的规则匹配将 php 转发给 php-fpm 处理后获取结果然后返回给客户端,转发模式可以通过 unix sock 或 tcp socket 方式。

相关推荐:《Nginx教程》

百度了好多文章我是没遇到一个能完整的而且正确的把 nginx 和 php 结合的配置讲述的较为正确的,这里总结了下最基本的 nginx + php 的模式配置,以及隐藏 index.php 和 开启 pathinfo 模式的方法。

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

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

http 配置

nginx.conf# nginx main configureuser  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 即可,二者选其一

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.socklisten = 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-redirectfastcgi_param  REDIRECT_STATUS    200;

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

以上就是nginx隐藏index.php和开启pathinfo模式的配置方法详解的详细内容,更多请关注24课堂在线网其它相关文章!

本文来自网络,不代表24小时课堂在线立场,转载请注明出处:https://www.24ketang.cn/33943.html

为您推荐

返回顶部