nginx.conf示例:
#window系统下一般不用配置用户
#user nobody;
#工作的子进程数量(通常等于CPU数量或者2倍于CPU)
worker_processes 4;
#error_log logs/error.log;
#error_log logs/error.log notice;
#错误日志存放路径
error_log logs/error.log info;
#指定pid存放文件 里面记录的是nginx的主进程的ID号
pid logs/nginx.pid;
events {
#使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue,window下不指定。
#use epoll;
#允许最大连接数
worker_connections 10240;
}
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 logs/access.log;
#access_log off;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_header_timeout 20;
client_body_timeout 20;
reset_timedout_connection on;
send_timeout 20;
proxy_read_timeout 60;
proxy_send_timeout 30;
proxy_buffer_size 32k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 60;
#压缩配置
gzip on;
gzip_disable "msie6";
gzip_min_length 1k;
gzip_buffers 4 16k;
# gzip_static on;
gzip_proxied any;
gzip_http_version 1.1;
#压缩等级,等级可以是1-9之间的任意数值 9是最慢但是压缩比最大的
gzip_comp_level 3;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_vary on;
open_file_cache max=100000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
#include /etc/nginx/conf.d/*.conf;
#include /etc/nginx/sites-enabled/*;
upstream localhost {
# ip_hash根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。
#同一机器在多网情况下,路由切换,ip可能不同
#weight=4
#ip_hash;
#server 10.22.49.20:8081 max_fails=2 weight=1;
#server 10.22.49.20:8082 max_fails=2 weight=1;
server 10.22.49.20:8081;
server 10.22.49.20:8082;
#jvm_route $cookie_JSESSIONID|sessionid reverse;
}
server {
listen 80;
server_name localhost;
charset utf-8;
#日志文件,如果不需要记录可以注释掉
access_log logs/host.access.log;
#/vas/开头的访问地址需要被代理,其他的 cas vms 等不代理,不进行负载均衡
#location ~ ^/(vas)/ {
#所有的访问都被代理
location / {
#需要代理的服务地址
proxy_pass http://localhost;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 3;
proxy_send_timeout 30;
proxy_read_timeout 30;
root html;
index index.html index.htm;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
proxy_pass http://10.22.49.20:8081;
root /webroot/res/;
}
location ~* \.(gif|jpg|jpeg|png|ico)$ {
access_log off;
#通过expires指令输出Header头来实现本地缓存
expires 15h;
}
location ~* \.(css|js)$ {
access_log off;
expires 1h;
}
#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;
# }
#}
}
常用命令:
start nginx
//启动
nginx -s stop
// 停止nginx
nginx -s reload
// 重新加载配置文件
nginx -s quit
// 退出nginx
高层的配置:
worker_processes
定义了nginx对外提供web服务时的worker进程数。最优值取决于许多因素,包括(但不限于)CPU核的数量、存储数据的硬盘数量及负载模式。不能确定的时候,将其设置为可用的CPU内核数将是一个好的开始(设置为“auto”将尝试自动检测它)。
Events模块:
worker_connections
设置一个worker进程同时打开的最大连接数。最大客户数也由系统的可用socket连接数限制(~ 64K),所以设置不切实际的高没什么好处。
HTTP 模块:
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 60;
sendfile
可以让sendfile()发挥作用。sendfile()可以在磁盘和TCP socket之间互相拷贝数据(或任意两个文件描述符)。Pre-sendfile是传送数据之前在用户空间申请数据缓冲区。之后用read()将数据从文件拷贝到这个缓冲区,write()将缓冲区数据写入网络。sendfile()是立即将数据从磁盘读到OS缓存。因为这种拷贝是在内核完成的,sendfile()要比组合read()和write()以及打开关闭丢弃缓冲更加有效(更多有关于sendfile)。
tcp_nopush
告诉nginx在一个数据包里发送所有头文件,而不一个接一个的发送。
tcp_nodelay
告诉nginx不要缓存数据,而是一段一段的发送–当需要及时发送数据时,就应该给应用设置这个属性,这样发送一小块数据信息时就不能立即得到返回值。
keepalive_timeout
给客户端分配keep-alive链接超时时间。服务器将在这个超时时间过后关闭链接。我们将它设置低些可以让ngnix持续工作的时间更长。
client_header_timeout 20;
client_body_timeout 20;
reset_timedout_connection on;
send_timeout 20;
client_header_timeout
和client_body_timeout
设置请求头和请求体(各自)的超时时间。我们也可以把这个设置低些。
reset_timeout_connection
告诉nginx关闭不响应的客户端连接。这将会释放那个客户端所占有的内存空间。
send_timeout
指定客户端的响应超时时间。这个设置不会用于整个转发器,而是在两次客户端读取操作之间。如果在这段时间内,客户端没有读取任何数据,nginx就会关闭连接。
gzip on;
gzip_disable "msie6";
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_static on;
gzip_proxied any;
gzip_http_version 1.1;
gzip_comp_level 3;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_vary on;
gzip
是告诉nginx采用gzip压缩的形式发送数据。这将会减少我们发送的数据量。
gzip_disable
为指定的客户端禁用gzip功能。我们设置成IE6或者更低版本以使我们的方案能够广泛兼容。
gzip_static
告诉nginx在压缩资源之前,先查找是否有预先gzip处理过的资源。这要求你预先压缩你的文件(在这个例子中被注释掉了),从而允许你使用最高压缩比,这样nginx就不用再压缩这些文件了。
gzip_proxied
允许或者禁止压缩基于请求和响应的响应流。我们设置为any,意味着将会压缩所有的请求。
gzip_min_length
设置对数据启用压缩的最少字节数。如果一个请求小于1000字节,我们最好不要压缩它,因为压缩这些小的数据会降低处理此请求的所有进程的速度。
gzip_comp_level
设置数据的压缩等级。这个等级可以是1-9之间的任意数值,9是最慢但是压缩比最大的。设置3-5,是一个比较折中的设置。
gzip_type
设置需要压缩的数据格式。上面例子中已经有一些了,你也可以再添加更多的格式。
upstream localhost
upstream localhost {
# ip_hash根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。同一机器在多网情况下,路由切换,ip可能不同
# weight 权重分配
#server 10.22.49.20:8081 max_fails=2 weight=2;
#server 10.22.49.20:8082 max_fails=2 weight=1;
}
SERVER 模块:
listen 80;
server_name localhost;
charset utf-8;
access_log logs/host.access.log;
listen
监听端口
server_name
自己制定要跳转的域名
charset utf-8
字符集
access_log
单独的access_log文件
#/vas/开头的访问地址需要被代理,不进行负载均衡
#location ~ ^/(vas)/ {
#"/"表示所有的访问都被代理
location / {
#此处配置的域名必须与upstream的域名一致,才能转发。
proxy_pass http://hostname;
proxy_set_header X-Real-IP $remote_addr;
}