当前位置: 首页 > 后端技术 > PHP

Nginx和PHP的配置

时间:2023-03-29 22:22:09 PHP

采用了nginx+php作为webserver的架构模式,目前应用比较广泛。但是,第一步需要实现的是如何让nginx正确调用php。由于nginx调用php不像调用静态文件那么直接,需要动态执行php脚本。所以涉及到nginx.conf文件的配置。对于新手来说,这一步一般需要上网搜索资料。对于普通有经验的用户来说,很多同学都没有搞明白为什么要这样配置。本文的主要内容是如何在nginx服务器中正确配置PHP的调用方式,以及配置的基本原则。1、修改nginx配置文件的配置文件位置Nginx配置文件默认位置为:/etc/nginx/nginx.conf在我的环境中nginx.conf在/etc/nginx/nginx中用vim打开nginx.conf文件。conf/etc/nginx/nginx.confetc/nginx/nginx.conf配置文件分析#nginx运行用户名usernginx;#nginx启动进程,一般设置为等于cpu个数,这里是automaticworker_processesauto;#errorlog文件位置error_log/var/log/nginx/error.log;#pid文件地址,记录了nginx的pid,方便进程管理pid/run/nginx.pid;#加载动态模块。参见/usr/share/nginx/README.dynamic。#用于加载其他动态模块模块配置包括/usr/share/nginx/modules/*.conf;#工作模式和连接数上限事件{#每个worker_processes的最大并发连接数#并发连接总数:worker_processes*worker_connectionsworker_connections1024;}#提供http服务相关的一些配置参数,类似mailhttp{#设置日志格式log_formatmain'$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.logmain;#这部分下面会单独说明#设置nginx是否使用sendfile函数输出文件发送文件;#当数据包最大时发送一个包(使用Nagle算法)tcp_nopushon;#立即发送数据包(禁用Nagle算法)tcp_nodelayon;#链接超时keepalive_timeout65;#这个我也不知道...types_hash_max_size2048;#引入文件扩展名和文件类型映射表include/etc/nginx/mime.types;#默认文件类型default_typeapplication/octet-stream;#从/etc/nginx/conf.d目录加载模块化配置文件。#有关详细信息,请参阅http://nginx.org/en/docs/ngx_core_module.html#include#。包括/etc/nginx/conf.d/*.conf;#HTTP服务支持多个虚拟主机#每个虚拟主机对应一个服务器配置项#配置项包含与虚拟主机相关的配置。服务器{#监听端口80default_server;听[::]:80默认服务器;#访问域名server_name_;#默认网站根目录(www目录)root/usr/share/nginx/html;#加载默认服务器的配置文件block.include/etc/nginx/default.d/*.conf;#默认请求位置/{}#错误页面(404)error_page404/404.html;location=/40x.html{}#错误页面(50X)error_page500502503504/50x.html;location=/50x.html{}}}注意事项1.关于error_log,可以设置日志的类型(记录什么级别的信息):debug,info,notice,warn,error,crit几种2.关于sendfile一般网络传输过程硬盘>>内核缓冲区>>用户缓冲区>>内核socket缓冲区>>协议栈使用sendfile硬盘>>内核缓冲区(快速复制到kernelsocket缓冲区)>>协议栈后可以显着提高传输性能.3.tcp_nopush和tcp_nodelaytcp_nopush仅在启用sendfile时有效。开启tcp_nopush后,程序收到数据包后不会立即发送出去,而是等待数据包最大时发送一次,可以缓解网络拥堵。(nagleization)相反,tcp_nodelay立即发出数据包。phpfastcgiconfiguration解析配置文件,开始配置环境。因为只是一个PHP服务器配置,而且只用到一个端口,所以只需要更改服务器部分,在vim中点击'i'进入编辑模式server{listen80default_server;听[::]:80默认服务器;写你的域名server_name192.168.17.26;#默认网站根目录(www目录)root/var/www/;#加载默认服务器块的配置文件。包括/etc/nginx/default.d/*.conf;location/{#改变定义主页的索引文件名indexindex.phpindex.htmlindex.htm;}error_page404/404.html;location=/40x.html{}error_page500502503504/50x.html;location=/50x.html{}#此处新增#PHP脚本请求全部转发给FastCGI处理。使用FastCGI协议的默认配置。#Fastcgi服务器与程序(PHP、Python)的通信协议。location~\.php${#设置监听端口fastcgi_pass127.0.0.1:9000;#设置nginx默认主页文件(上面已经设置,可以删除)fastcgi_indexindex.php;#设置脚本文件请求的路径fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;#引入fastcgi配置文件includefastcgi_params;}}修改完成后,将vim编辑器切换到普通半模式(Esc),然后输入:wq保存退出然后重启Nginx服务servicenginxrestarttoconfigure就成功了,但是上面的配置是只是nginx配置的一部分,更多的内容需要继续学习。测试我们可以通过以下方法判断Nginx配置是否成功。在Nginx网站根目录(/var/www/)下新建一个php文件,命名为minephp_info.php。内容如下:保存,然后在浏览器中访问localhost:8011/xx.php,即可在网页中显示helloworld。3、外网访问内网设置外网IP:http://58.62.21.107:8382映射内网服务器IP192.168.17.56的82端口,即:192.168.17.56:82,需要在nginx.conf配置文件中开启82端口用于外网访问,即如下配置:[root@ceshiwww]#cat/etc/nginx/nginx.conf#更多配置见:#*官方英文文档:http://nginx.org/en/docs/#*官方俄文文档:http://nginx.org/ru/docs/usernginx;worker_processesauto;error_log/var/log/nginx/error.log;pid/run/nginx.pid;#加载动态模块。见/usr/share/nginx/README.dynamic.include/usr/share/nginx/modules/*.conf;events{worker_connections1024;}http{log_formatmain'$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.logmain;发送文件;tcp_nopush上;tcp_nodelayon;keepalive_timeout65;types_hash_max_尺寸2048;包括/etc/nginx/mime.types;default_type应用程序/八位字节流;#从/etc/nginx/conf.d目录加载模块化配置文件。#参见http://nginx.org/en/docs/ngx_core_module.html#include#获取更多信息。include/etc/nginx/conf.d/*.conf;服务器{听82default_server;#服务器端口,必须与外网映射保持一致listen[::]:82default_server;#服务器端口,需要和外网映射一致server_name192.168.17.56;根/数据/www/;#加载默认服务器块的配置文件。包括/etc/nginx/default.d/*.conf;位置/{}error_page404/404.html;location=/40x.html{}error_page500502503504/50x.html;location=/50x.html{}#所有的PHP脚本请求都被转发给FastCGI进行处理。使用FastCGI协议默认配置。#Fastcgi服务器与程序(PHP、Python)的通信协议。location~\.php${#设置监听端口fastcgi_pass127.0.0.1:9000;#设置nginx默认主页文件(上面已经设置,可以删除)fastcgi_indexindex.php;#设置脚本文件请求的路径fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;#引入fastcgi配置文件includefastcgi_params;}}#启用TLS的服务器的设置。##server{#listen443sslhttp2default_server;#listen[::]:443sslhttp2default_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_cacheshared:SSL:1m;#ssl_session_timeout10m;#ssl_ciphersHIGH:!aNULL:!MD5;#ssl_prefer_server_cipherson;###加载默认服务器块的配置文件。#include/etc/nginx/default.d/*.conf;##location/{#}##error_page404/404.html;#location=/40x.html{#}##error_page500502503504/50x.html;#location=/50x.html{#}#}}参考文章:Nginx安装配置(PHP)nginx+php的配置及原理