采用nginx+php做为webserver的架构模式,在现现在运用至关普遍。然而第一步须要实现的是如何让nginx正确的调用php。因为nginx调用php并非如同调用一个静态文件那么直接简单,是须要动态执行php脚本。因此涉及到了对nginx.conf文件的配置。这一步对新手而言通常须要网上查资料,对于通常的熟手而言,也有很多同窗并无搞透彻为什么要如此这般配置。本文的主要内容为如何在nginx server中正确配置php调用方法,以及配置的基本原理。
Nginx的配置文件默认位置为:/etc/nginx/nginx.conf
php
在个人环境中 nginx.conf
在 /etc/nginx/nginx.conf
html
使用vim打开文件nginx.confnode
vim /etc/nginx/nginx.conf
# nginx运行的用户名 user nginx; # nginx启动进程,一般设置成和cpu的数量相等,这里为自动 worker_processes auto; # errorlog文件位置 error_log /var/log/nginx/error.log; # pid文件地址,记录了nginx的pid,方便进程管理 pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic. # 用来加载其余动态模块的配置 include /usr/share/nginx/modules/*.conf; # 工做模式和链接数上限 events { # 每一个worker_processes的最大并发连接数 # 并发总数:worker_processes*worker_connections worker_connections 1024; } # 与提供http服务相关的一些配置参数相似的还有mail http { # 设置日志的格式 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; # access_log记录访问的用户、页面、浏览器、ip和其余的访问信息 access_log /var/log/nginx/access.log main; # 这部分下面会单独解释 # 设置nginx是否使用sendfile函数输出文件 sendfile on; # 数据包最大时发包(使用Nagle算法) tcp_nopush on; # 马上发送数据包(禁用Nagle算法) tcp_nodelay on; # 连接超时时间 keepalive_timeout 65; # 这个我也不清楚... types_hash_max_size 2048; # 引入文件扩展名与文件类型映射表 include /etc/nginx/mime.types; # 默认文件类型 default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; # http服务上支持若干虚拟主机。 # 每一个虚拟主机一个对应的server配置项 # 配置项里面包含该虚拟主机相关的配置。 server { # 端口 listen 80 default_server; listen [::]:80 default_server; # 访问的域名 server_name _; # 默认网站根目录(www目录) root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; # 默认请求 location / { } # 错误页(404) error_page 404 /404.html; location = /40x.html { } # 错误页(50X) error_page 500 502 503 504 /50x.html; location = /50x.html { } } }
一、关于error_log 能够设置log的类型(记录什么级别的信息)有:debug、info、notice、warn、error、crit几种nginx
二、关于sendfile
通常的网络传输过程
硬盘 >> kernel buffer >> user buffer>> kernel socket buffer >>协议栈
使用sendfile后
硬盘 >> kernel buffer (快速拷贝到kernelsocket buffer) >>协议栈
能够显著提升传输性能。web
三、tcp_nopush和tcp_nodelay
tcp_nopush只有在启用了sendfile时才起做用,
在启用tcp_nopush后,程序接收到了数据包后不会立刻发出,而是等待数据包最大时一次性发出,能够缓解网络拥堵。(Nagle化)
相反tcp_nodelay则是当即发出数据包.算法
分析完了配置文件后开始配置环境。vim
由于只是配置PHP的服务器,并且只使用一个端口因此只须要改动server部分浏览器
在vim中点击‘i’进入编辑模式服务器
server { listen 80 default_server; listen [::]:80 default_server; # 这里改动了,也能够写你的域名 server_name 192.168.17.26; # 默认网站根目录(www目录) root /var/www/; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { # 这里改动了 定义首页索引文件的名称 index index.php index.html index.htm; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } # 这里新加的 # PHP 脚本请求所有转发到 FastCGI处理. 使用FastCGI协议默认配置. # Fastcgi服务器和程序(PHP,Python)沟通的协议. location ~ \.php$ { # 设置监听端口 fastcgi_pass 127.0.0.1:9000; # 设置nginx的默认首页文件(上面已经设置过了,能够删除) fastcgi_index index.php; # 设置脚本文件请求的路径 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 引入fastcgi的配置文件 include fastcgi_params; } }
修改完成后将vim编辑器切换到通常一半模式(Esc),而后输入:wq保存退出。网络
以后重启Nginx服务
service nginx restart
以上就配置成功了,可是上面的配置只是nginx配置部分,更多的内容须要继续学习。
咱们能够经过下面的方法判断Nginx配置是否成功。
在Nginx的网站根目录(/var/www/)下建立一个php文件,随便起名个人是php_info.php
内容以下:
<?php // 顺即可以看一下php的扩展全不全 phpinfo();
进入你的网站看看能不能打开文件
你的ip/文件名 例如:192.168.17.26/php_info.php
ok,咱们能够看到配置成功了。
上边咱们已经配置成功了,如今咱们来看下具体的原理。
首先简单的讲一讲原理,目前主流的nginx+php的运行原理以下:
一、nginx的worker进程
直接管理每个请求到nginx的网络请求。
二、对于php而言,因为在整个网络请求的过程当中php是一个cgi程序的角色,因此采用名为php-fpm的进程管理程序
来对这些被请求的php程序进行管理。php-fpm程序也如同nginx同样,须要监听端口,而且有master和worker进程。worker进程直接管理每个php进程。
三、关于fastcgi:fastcgi是一种进程管理器,管理cgi进程。市面上有多种实现了fastcgi功能的进程管理器,php-fpm就是其中的一种。再提一点,php-fpm做为一种fast-cgi进程管理服务,会监听端口,通常默认监听9000端口,而且是监听本机
,也就是只接收来自本机的端口请求,因此咱们一般输入命令 netstat -nlpt|grep php-fpm 会获得:
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 1057/php-fpm
这里的127.0.0.1:9000 就是监听本机9000端口的意思。
四、关于fastcgi的配置文件,目前fastcgi的配置文件通常放在nginx.conf同级目录下,配置文件形式,通常有两种:fastcgi.conf 和 fastcgi_params。不一样的nginx版本会有不一样的配置文件,这两个配置文件有一个很是重要的区别:fastcgi_parames文件中缺乏下列配置:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
咱们能够打开fastcgi_parames文件加上上述行,也能够在要使用配置的地方动态添加。使得该配置生效。
五、当须要处理php请求时,nginx的worker进程会将请求移交给php-fpm的worker进程进行处理,也就是最开头所说的nginx调用了php,其实严格得讲是nginx间接调用php
。
了解了上面的这五个简单原理,在nginx中配置php调用方法就变得易如反掌。
配置文件详解:
server { listen 8011; server_name test.cn; location ~ \.php?.*$ { root /share/test; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
一、第一个大括号 server{ }:没必要多说,表明一个独立的server,
二、listen 8011:表明该server监听8011端口
三、location ~ .php?.*${ }:表明一个能匹配对应uri的location,用于匹配一类uri,并对所匹配的uri请求作自定义的逻辑、配置。这里的location,匹配了全部带.php的uri请求,例如:http://192.168.244.128:8011/test.php/asdasd http://192.168.244.128:8011/index.php等
四、root /share/test:请求资源根目录,告诉匹配到该location下的uri到/share/teset文件夹下去寻找同名资源。
五、fastcgi_pass 127.0.0.1:9000:这行开始是本文的重点:这行代码的意思是,将进入到该location内的uri请求看作是cgi程序,并将请求发送到9000端口,交由php-fpm处理。
六、fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; :这行配置意思是:动态添加了一行fastcgi配置,配置内容为SCRIPT_FILENAME,告知管理进程,cgi脚本名称。因为个人nginx中只有fastcgi_params文件,没有fastcgi.conf文件,因此要使php-fpm知道SCRIPT_FILENAME的具体值,就必需要动态的添加这行配置。
七、include fastcgi_params; 引入fastcgi配置文件
以上就是最简洁版的nginx启动php脚本的最简配置,当重启nginx以后,在/share/test目录下建立一个xx.php文件,输入<?php echo "hello world"; ?>保存,而后在浏览器中访问localhost:8011/xx.php 就能够在网页上显示hello world了。
外网IP:http://58.62.21.107:8382 映射内网服务器IP 192.168.17.56 的 82 端口,即:192.168.17.56:82,须要在nginx.conf
配置文件中开放 82 端口给 外网访问,即下面的配置:
[root@ceshi www]# cat /etc/nginx/nginx.conf # For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; server { listen 82 default_server; # 服务器端口,和外网映射的需保持一致 listen [::]:82 default_server; # 服务器端口,和外网映射的需保持一致 server_name 192.168.17.56; root /data/www/; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } # PHP 脚本请求所有转发到 FastCGI处理. 使用FastCGI协议默认配置. # Fastcgi服务器和程序(PHP,Python)沟通的协议. location ~ \.php$ { # 设置监听端口 fastcgi_pass 127.0.0.1:9000; # 设置nginx的默认首页文件(上面已经设置过了,能够删除) fastcgi_index index.php; # 设置脚本文件请求的路径 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 引入fastcgi的配置文件 include fastcgi_params; } } # Settings for a TLS enabled server. # # server { # listen 443 ssl http2 default_server; # listen [::]:443 ssl http2 default_server; # server_name _; # root /usr/share/nginx/html; # # ssl_certificate "/etc/pki/nginx/server.crt"; # ssl_certificate_key "/etc/pki/nginx/private/server.key"; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 10m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # # # Load configuration files for the default server block. # include /etc/nginx/default.d/*.conf; # # location / { # } # # error_page 404 /404.html; # location = /40x.html { # } # # error_page 500 502 503 504 /50x.html; # location = /50x.html { # } # } }