######
### Description: The config file of Nginx, no-www redircting, gzip functions
### Author: licong 2017.04.14 https://www.lcgod.com/
######
user nginx nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid /var/run/nginx/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
charset UTF-8;
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;
#keepalive_timeout 0;
keepalive_timeout 65;
#隐藏Nginx版本信息,禁止网站目录浏览
server_tokens off;
autoindex off;
#当FastCGI后端服务器处理请求给出http响应码为4xx和5xx时,就转发给nginx
fastcgi_intercept_errors on;
#关于fastcgi的配置
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
#支持gzip压缩
gzip on;
gzip_min_length 1k;
gzip_buffers 16 64k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript text/css application/javascript text/javascript image/jpeg image/gif image/png application/xml application/json;
gzip_vary on;
gzip_disable "MSIE [1-6].(?!.*SV1)";
#
server {
listen *:80;
server_name www.lcgod.com;
#301重定向
if ($host != 'www.lcgod.com' ) {
rewrite ^(.*)/$ http://www.lcgod.com/$1 permanent;
}
# 不产生日志
access_log off;
location ~* ^/(favicon.ico|robots.txt)$ {
root html;
expires max;
log_not_found off;
break;
}
#设置网站根目录
root /home/www;
index index.php index.html;
#access_log /var/log/nginx/log/host.access.log main;
#设置css/javascript/图片等静态资源的缓存时间
location ~ .*\.(css|js|ico|png|gif|jpg|json|mp3|mp4|flv|swf)(.*) {
expires 60d;
}
# include /etc/nginx/default.d/*.conf;
# 设置网站的config文件不被访问,保证安全
location = /config.inc.php{
deny all;
}
#URLRewrite
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php/s=$1 last;
break;
}
}
#设置访问favicon.ico时不产生日志
location = /favicon.ico {
access_log off;
}
#设置40系列错误的应答文件为40x.html
error_page 400 401 402 403 404 /40x.html;
location = /40x.html {
root html/404;
index index.html index.htm;
}
#设置50系列错误的应答文件为50x.html
#
error_page 500 501 502 503 504 /50x.html;
location = /50x.html {
root html/502;
index index.html index.htm;
}
## 设置Nginx和php通信机制为tcp的socket模式,而不是直接监听9000端口
location ~ .*\.php(\/.*)*$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/www/$fastcgi_script_name;
include fastcgi_params;
}
}
}