/ 中存储网

Nginx服务器配置文件讲解

2014-04-01 08:19:01 来源:ITJS.CN
记录一下,省得每次都到处找。 几个关键地方都有注释,基本看得懂了

nginx.conf

user  nobody nobody;

worker_processes  4; #根据cpu个数来确定 #error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

pid        logs/nginx.pid;

worker_rlimit_nofile 65535;

events {

use epoll;

worker_connections  65535;

}

http {

include       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  logs/access.log  main;

access_log  off;

server_names_hash_bucket_size  128;

client_header_buffer_size      32k;

large_client_header_buffers    4 32k;

client_max_body_size           8m;

sendfile        on;

tcp_nopush      on;

keepalive_timeout  60;

tcp_nodelay on;

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;

fastcgi_intercept_errors     on; #php文件不存在也返回404,无此参数会返回空白页

gzip on;

gzip_static        on; #启用此参数nginx编译时需要--with-http_gzip_static_module,用于cdn。否则cdn不能缓存

gzip_disable       "MSIE [1-6].|Baiduspider"; #IE6不支持压缩,另外百度蜘蛛比较2,不会自解压

gzip_proxied       any; #当nginx作为反向代理时,无条件压缩

gzip_min_length    1k;

gzip_buffers       4 16k;

gzip_http_version  1.1;

gzip_comp_level    6;

gzip_types         text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

gzip_vary on;

server {

listen       80;

server_name  xs8.cn www.xs8.cn;

if ($host != 'www.xs8.cn') {

rewrite ^/(.*)$ http://www.xs8.cn/$1 permanent;

}

#以下仅用于CI框架

if (!-f $request_filename) {

rewrite ^/(.*)$ /index.php?$1 last;

}

index index.html index.htm index.php;

root  /data/www/xs8.cn;

location ~ .*.(php|php5)?$ {      

include fcgi.conf;

}

location ~ .*.(gif|jpg|jpeg|png|bmp|swf|flv)$ {

#valid_referers none blocked server_names; #防盗链时打开注释

#if ($invalid_referer) {return 403;} #防盗链时打开注释

expires      30d;

}

location ~ .*.(js|css)?$ {

expires      1h;

}

error_page    404    http://www.xs8.cn/;

access_log    off;

}

}

fcgi.conf

fastcgi_pass  127.0.0.1:9000;

fastcgi_index index.php; if ($request_filename ~* (.*).php) {

set $php_url $1;

}

if (!-e $php_url.php) {

return 403;

}

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;

fastcgi_param  SERVER_SOFTWARE    nginx;

fastcgi_param  QUERY_STRING       $query_string;

fastcgi_param  REQUEST_METHOD     $request_method;

fastcgi_param  CONTENT_TYPE       $content_type;

fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;

fastcgi_param  REQUEST_URI        $request_uri;

fastcgi_param  DOCUMENT_URI       $document_uri;

fastcgi_param  DOCUMENT_ROOT      $document_root;

fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  REMOTE_ADDR        $remote_addr;

fastcgi_param  REMOTE_PORT        $remote_port;

fastcgi_param  SERVER_ADDR        $server_addr;

fastcgi_param  SERVER_PORT        $server_port;

fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect

fastcgi_param  REDIRECT_STATUS    200;

nginx启动、停止、重载脚本

/etc/init.d/nginx

#!/bin/bash

# nginx Startup script for the Nginx HTTP Server

# this script create it by jackbillow at 2007.10.15.

# it is v.0.0.2 version.

# if you find any errors on this scripts,please contact jackbillow.

# and send mail to jackbillow at gmail dot com.

#

# chkconfig: - 85 15

# description: Nginx is a high-performance web and proxy server.

#              It has a lot of features, but it's not for everyone.

# processname: nginx

# pidfile: /var/run/nginx.pid

# config: /usr/local/nginx/conf/nginx.conf nginxd=/usr/local/nginx/sbin/nginx

nginx_config=/usr/local/nginx/conf/nginx.conf

nginx_pid=/usr/local/nginx/logs/nginx.pid

RETVAL=0

prog="nginx"

# Source function library.

. /etc/rc.d/init.d/functions

# Source networking configuration.

. /etc/sysconfig/network

# Check that networking is up.

[ ${NETWORKING} = "no" ] && exit 0

[ -x $nginxd ] || exit 0

# Start nginx daemons functions.

start() {

if [ -e $nginx_pid ];then

   echo "nginx already running...."

   exit 1

fi

   echo -n $"Starting $prog: "

   daemon $nginxd -c ${nginx_config}

   RETVAL=$?

   echo

   [ $RETVAL = 0 ] && touch $nginx_pid

   return $RETVAL

}

# Stop nginx daemons functions.

stop() {

echo -n $"Stopping $prog: "

killproc $nginxd

RETVAL=$?

echo

[ $RETVAL = 0 ] && rm -f $nginx_pid

}

# reload nginx service functions.

reload() {

echo -n $"Reloading $prog: "

#kill -HUP `cat ${nginx_pid}`

killproc $nginxd -HUP

RETVAL=$?

echo

}

# See how we were called.

case "$1" in

start)

start

;;

stop)

stop

;;

reload)

reload

;;

restart)

stop

start

;;

status)

status $prog

RETVAL=$?

;;

*)

echo $"Usage: $prog {start|stop|restart|reload|status|help}"

exit 1

esac

exit $RETVAL

安装自启动 chmod +x /etc/init.d/nginx

/sbin/chkconfig nginx on

/sbin/chkconfig -list nginx

用法 service nginx start

service nginx stop

service nginx restart

service nginx reload

/etc/init.d/nginx start

/etc/init.d/nginx stop

/etc/init.d/nginx restart

/etc/init.d/nginx reload