1.yum下载的配置文件
配置文件分析# 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_connectionsworker_connections 1024;}ttp {# 设置日志的格式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 {}}}
添加PHP只需修改server部分
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?.*$ { #nginx+PHP时添加以下几行root /share/test;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}}
1、第一个大括号 server{ }:不必多说,代表一个独立的server,
2、listen 8011:代表该server监听8011端口
3、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等
4、root /share/test:请求资源根目录,告诉匹配到该location下的uri到/share/teset文件夹下去寻找同名资源。
5、fastcgi_pass 127.0.0.1:9000:这行开始是本文的重点:这行代码的意思是,将进入到该location内的uri请求看做是cgi程序,并将请求发送到9000端口,交由php-fpm处理。
6、fastcgi_param SCRIPT_FILENAME fastcgi_script_name; :这行配置意思是:动态添加了一行fastcgi配置,配置内容为SCRIPT_FILENAME,告知管理进程,cgi脚本名称。由于我的nginx中只有fastcgi_params文件,没有fastcgi.conf文件,所以要使php-fpm知道SCRIPT_FILENAME的具体值,就必须要动态的添加这行配置。
7、include fastcgi_params; 引入fastcgi配置文件
以上就是最简洁版的nginx启动php脚本的最简配置,当重启nginx之后,在/share/test目录下创建一个xx.php文件,输入<?php echo “hello world”; ?>保存,然后在浏览器中访问localhost:8011/xx.php 就可以在网页上显示hello world了。
