我不是伊戈尔·塞索耶夫,所以nginx的使用只存在于应用层,看了张宴的《实战Nginx取代Apache的高性能的Web服务器》做做笔记共勉。
一、windows环境
在cmd窗口执行下面的命令,启动nginx,一般nginx都是在linux环境下,这里用windows主要是为了验证一些我感兴趣的东西。
可以按照其他DOS命令
nginx -s stop (其他选项 quit reopen reload)
1、nginx.conf
#user nobody;
worker_processes 8;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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"';
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;
upstream fay_p {
server 127.0.0.1:3000;
}
include extra/base.conf;
include extra/gzip.conf;
include extra/fastcgi.conf;
#include extra/proxy.conf;
include extra/fay.conf;
}
2、fastcgi.conf
CGI(Common Gateway Interface,通用网关接口),他是其他程序与web服务器之间的接口标准,而FastCGI主要解决CGI fork-and-execute 模式的耗时问题。
下图来自1.7.2 Nginx+FastCGI运行原理
#ceshi_config
server_names_hash_bucket_size 128;
client_header_buffer_size 64k;
large_client_header_buffers 8 32k;
#指定链接到后端的超时时间
fastcgi_connect_timeout 300;
#向fastcgi 发送请求的超时时间,指两次捂手后向fastcgi 传输请求的超时时间
fastcgi_send_timeout 300;
#fastcgi 应答超时时间
fastcgi_read_timeout 300;
#fastcgi 应答需要多大的缓冲区
#fastcgi_buffer_size 64k;
fastcgi_buffer_size 128k;
#fastcgi 应答 指定本地需要用多少个和多大的缓冲区来缓冲
#fastcgi_buffers 4 64k;
fastcgi_buffers 8 128k;
#fastcgi 繁忙的时候buffers 大小
#fastcgi_busy_buffers_size 128k;
fastcgi_busy_buffers_size 256k;
#fastcgi 临时文件大小
#fastcgi_temp_file_write_size 128k;
fastcgi_temp_file_write_size 256k;
3 gzip.conf
######################################
#开启压缩
gzip on;
#设置对数据启用压缩的最少字节数。大于1k才压缩
gzip_min_length 1k;
#打开 4个单位为16k 的缓存流用作压缩
gzip_buffers 4 16k;
#gzip_http 版本选择默认即可 现在的版本基本支持
#gzip_http_version 1.0;
# 设置数据的压缩等级。这个等级可以是1-9之间的任意数值,9是最慢但是压缩比最大的。
gzip_comp_level 3;
# 设置需要压缩的数据格式 文本 JavaScript 等。
#gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_types text/css text/xml application/javascript application/atom+xml application/rss+xml text/plain ;
# vary header 支持, 该选项让前端缓存服务器能缓存经过gzip压缩界面
gzip_vary on;
# 为指定的客户端禁用gzip功能。我们设置成IE6或者更低版本以使我们的方案能够广泛兼容。
gzip_disable "MSIE [1-6]\.";
#允许或者禁止压缩基于请求和响应的响应流。我们设置为any,意味着将会压缩所有的请求
gzip_proxied any;
#fire fox 安装firebug yslow 看看有没有压缩
######################################
4 base.conf
#优化hash 表
# 服务器名称哈希表的最大值,更多信息请参考nginx部分优化。
server_names_hash_max_size 512 ;
######################################
#开启高效的传输模式
sendfile on;
# 告诉nginx在一个数据包里发送所有头文件,而不一个接一个的发送
tcp_nopush on;
tcp_nodelay on;
######################################
#隐藏版本信息
server_tokens off;
######################################
# 设置连接超时
#设置客户端连接保持会话的超时世间,超过这个世间,服务器关闭该连接
keepalive_timeout 60;
#设置客户端请求头读取超时世间,如果超过这个世间,客户端没有发送任何数据,nginx 将返回 "Request time out 408"
client_header_timeout 15;
#客户端请求主体读取超时世间,客户端没有发送任何数据,nginx 将返回 "Request time out 408"
client_body_timeout 15;
#客户端的响应超时时间。这个设置不会用于整个转发器,而是在两次客户端读取操作之间。如果在这段时间内,客户端没有读取任何数据,nginx就会关闭连接。
send_timeout 15;
######################################
#########文件上传#####################################
client_max_body_size 50m;
##缓冲区代理缓冲用户端请求的最大字节数
client_body_buffer_size 256k;
##############################################
5 fay.conf
这个是自定义的工程
server {
listen 80;
client_max_body_size 100M;
server_name fay.com;
charset utf-8,gbk;
access_log logs/fay.access.log main;
root /var/www/html/fay;
error_page 500 502 503 504 /50x.html;
location / {
proxy_pass http://fay_p;
}
#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;
}
location ~ .*\.(gif|html|htm|bmp|png|exe|JPG|GIF|CSS|BMP|PNG|EXE|xml|XML|zip|ZIP|svg|flv|xml)$
{
expires 30d;
#root /var/www/html/fay;
proxy_pass http://fay_p;
}
location ~ .*\.(gzcss|css|js|JS|gzjs)$
{
expires 1h;
#root /var/www/html/fay;
proxy_pass http://fay_p;
}
}
二 linux环境
1 编译安装下启动配置
注意调整nginx的目录即可,此种适用于编译安装
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# 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=/application/nginx/nginx/sbin/nginx
nginx_config=/application/nginx/nginx/conf/nginx.conf
nginx_pid=/var/run/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 /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/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
2 yum安装
yum -y install与yum install有什么不同
# 安装, 安装后nginx配置信息位于/etc/nginx目录下
yum -y install nginx
# 启动
service nginx start/stop/restart
# 卸载
yum -y remove nginx