作为一名 PHP 开发人员,我们经常会和 Nginx 服务器打交道,所以理解 Nginx 的配置是必不可少的,以便于更好的配置和使用。下面我以 Nginx 1.14.2 的配置文件为例进行介绍,Os为 CentOS。
#运行nginx的用户
user nginx;
#开启进程,设置成cpu核数相同即可
worker_processes 1;
#错误日志存放位置 'warn级别'
error_log /var/log/nginx/error.log warn;
#pid文件位置
pid /var/run/nginx.pid;
#工作模式和连接数上线
events {
#epoll是多路复用IO中的一种方式,可以大大提高nginx的性能
use epoll;
#单个worker_processes 的最大并发链接数
worker_connections 1024;
}
http {
#设定mime类型
include /etc/nginx/mime.types;
default_type application/octet-stream;
#设定日志格式
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;
#指定 nginx 是否调用 sendfile 函数输出文件,对于普通应用,必须设为 on,如果用来进行下载等应用,可设置为 off。
sendfile off;
#tcp_nopush on;
#链接超时时间
keepalive_timeout 65;
#tcp_nodelay on;
#开启gzip压索,可以提升页面加载速度
gzip on;
#加载自定义配置文件
include /etc/nginx/conf.d/*.conf;
}
虚拟主机配置文件
server {
#监听的端口号
listen 80;
#定义与你域名
server_name blog.ihege.top;
#定义项目代码根目录
root /www/blog/public;
#设置字符集
charset utf-8;
#默认请求处理
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?$query_string;
}
#错误页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
#php脚本使用fastcgi进行处理
location ~* \.php$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#禁止访问分布式配置文件 .htaccess
location ~ /\.ht {
deny all;
}
}
文章同步发布在我的个人博客中,传送门Hesunfly Blog
网友评论