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 配置。
立即學習“PHP免費學習筆記(深入)”;
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 即可,二者選其一
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 服務