Nginx的基本配置与优化
一、Nginx配置示例
vim /etc/nginx/nginx.conf
……………………
# 使用的用户和用户组
user nginx nginx;
# 指定工作衍生进程数(一般等于CPU的总核数)
worker_processes 2;
# 错误日志的存放路径, 错误日志记录级别可以为{debug|info|notice|warn|error|crit}
error_log /var/log/nginx/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
# 指定pid的存放路径
pid /usr/local/nginx/log/nginx.pid;
events {
# 使用的网络I/O模型, Linux系统推荐epoll模型,FreeBSD系统推荐使用kqueue模型
use epoll;
# 允许的连接数
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
# 设置客户端能够上传的文件大小
client_max_body_size 8m;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 60;
#gzip on;
log_format main '$remote_addr - $remote_user [$time_local] "$request"'
'$status $body_bytes_sent "$http_referer"'
'"$http_user_agent" "$http_x_forwarded_for"';
server {
listen 80;
server_name localhost;
#charset koi8-r;
#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 /usr/local/nginx/log/access.log main;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
注意:nginx1.12.0+版本需要 将log_format 放置到 server段外部,否则会报一个类似:nginx: [emerg] "log_format" directive is not allowed here in xxx 的错误。