原文链接: http://hzcsky.blog.51cto.com/1560073/1625354
http://os.51cto.com/art/201111/304611.htm
Nginx配置文件详细说明在此记录下Nginx服务器nginx.conf的配置文件说明, 部分注释收集与网络.
#运行用户
user www-data;
#启动进程,通常设置成和cpu的数量相等
worker_processes 1;#全局错误日志及PID文件
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;#工作模式及连接数上限
events {
use epoll; #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
worker_connections 1024;#单个后台worker process进程的最大并发链接数
# multi_accept on;
}#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型,类型由mime.type文件定义
include /etc/nginx/mime.types;
default_type application/octet-stream;
#设定日志格式
access_log /var/log/nginx/access.log;#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
#必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
sendfile on;
#tcp_nopush on;#连接超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
#开启gzip压缩
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";#设定请求缓冲
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;#设定负载均衡的服务器列表
upstream mysvr {
#weigth参数表示权值,权值越高被分配到的几率越大
#本机上的Squid开启3128端口
server 192.168.8.1:3128 weight=5;
server 192.168.8.2:80 weight=1;
server 192.168.8.3:80 weight=6;
}
server {
#侦听80端口
listen 80;
#定义使用www.xx.com访问
server_name www.xx.com;#设定本虚拟主机的访问日志
access_log logs/www.xx.com.access.log main;#默认请求
location / {
root /root; #定义服务器的默认网站根目录位置
index index.php index.html index.htm; #定义首页索引文件的名称fastcgi_pass www.xx.com;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}# 定义错误提示页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /root;
}#静态文件,nginx自己处理
location ~ ^/(p_w_picpaths|javascript|js|css|flash|media|static)/ {
root /var/www/virtual/htdocs;
#过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
expires 30d;
}
#PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
location ~ \.php$ {
root /root;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name;
include fastcgi_params;
}
#设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
}
#禁止访问 .htxxx 文件
location ~ /\.ht {
deny all;
}
}
}
以上是一些基本的配置,使用Nginx最大的好处就是负载均衡
如果要使用负载均衡的话,可以修改配置http节点如下:
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型,类型由mime.type文件定义
include /etc/nginx/mime.types;
default_type application/octet-stream;
#设定日志格式
access_log /var/log/nginx/access.log;#省略上文有的一些配置节点
#。。。。。。。。。。
#设定负载均衡的服务器列表
upstream mysvr {
#weigth参数表示权值,权值越高被分配到的几率越大
server 192.168.8.1x:3128 weight=5;#本机上的Squid开启3128端口
server 192.168.8.2x:80 weight=1;
server 192.168.8.3x:80 weight=6;
}upstream mysvr2 {
#weigth参数表示权值,权值越高被分配到的几率越大server 192.168.8.x:80 weight=1;
server 192.168.8.x:80 weight=6;
}#第一个虚拟服务器
server {
#侦听192.168.8.x的80端口
listen 80;
server_name 192.168.8.x;#对aspx后缀的进行负载均衡请求
location ~ .*\.aspx$ {root /root; #定义服务器的默认网站根目录位置
index index.php index.html index.htm; #定义首页索引文件的名称proxy_pass http://mysvr ;#请求转向mysvr 定义的服务器列表
#以下是一些反向代理的配置可删除.
proxy_redirect off;
#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,
proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传}
}
}
本文主要帮助大家能快速搭建一个可用的负载均衡环境.
首先是需要JBOSS服务器若干,具体搭建方法在此不做描述.
安装nginx,
shell代码
shell>> cd /opt
shell>> wget http://nginx.org/download/nginx-1.0.6.tar.gz
shell>> tar xzvf nginx-0.7.64.tar.gz
shell>> cd nginx-1.0.6
shell>> ./configure \
--user=nginx \
--group=nginx \
--prefix=/opt/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/tmp/nginx/client_body \
--http-proxy-temp-path=/tmp/nginx/proxy \
--http-fastcgi-temp-path=/tmp/nginx/fastcgi \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/subsys/nginx \
--with-http_stub_status_module这里解释一下:
# --user 是指启用程序所属用户
# --group 是指启动程序所属组
# --prefix 是指nginx安装目录(不是源代码目录)
# --sbin-path 是指nginx命令位置
# --conf-path 是指配置文件路径
# --error-log-path 是错误日志路径
# --http-log-path 是访问日志其他是一些临时文件的路径和做linux service需要用到的文件
# --with-http_stub_status_module 需要监控服务需安装此监控状态模块
然后:
shell>> cd /opt/nginx
shell>> make
shell>> make install
到此为止,NGINX已经可以正常启动了,我们可以cd到nginx安装目录执行 ./sbin/nginx 启动nginx.
但是我们如果想要把nginx做成一个服务,必须要写一个shell.
简单说一下,
# chkconfig: - 85 15 所有运行级别,启动优先级85, 关闭优先级15
# processname: 进程名称
# config: nginx配置文件位置
# config: 系统会优先找第一个,如果找不到再去找第二个
# pidfile: 进程ID存放文件,用来存放程序启动后的进程ID
# Source function library. linux常用的方法库,有兴趣可以去看看service XXX status 就使用了里边的一个方法
# Source networking configuration. 网络配置
#!/bin/sh # # nginx - this script starts and stops the nginx daemon # by haitao.tu at 2009-12-15 # email:tuhaitao@foxmail.com # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n {1}quot;Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n {1}quot;Stopping $prog: " killproc $prog retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest_q || configtest || return 6 stop start } reload() { configtest_q || configtest || return 6 echo -n {1}quot;Reloading $prog: " killproc $nginx -HUP echo } configtest() { $nginx -t -c $NGINX_CONF_FILE } configtest_q() { configtest >/dev/null 2>&1 } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } # Upgrade the binary with no downtime. upgrade() { local pidfile="/var/run/${prog}.pid" local oldbin_pidfile="${pidfile}.oldbin" configtest_q || configtest || return 6 echo -n {1}quot;Staring new master $prog: " killproc $nginx -USR2 retval=$? echo sleep 1 if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]]; then echo -n {1}quot;Graceful shutdown of old $prog: " killproc -p ${oldbin_pidfile} -QUIT retval=$? echo return 0 else echo {1}quot;Something bad happened, manual intervention required, maybe restart?" return 1 fi } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; force-reload|upgrade) rh_status_q || exit 7 upgrade ;; reload) rh_status_q || exit 7 $1 ;; status|status_q) rh_$1 ;; condrestart|try-restart) rh_status_q || exit 7 restart ;; *) echo {1}quot;Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart}" exit 2 esac
下面是NGINX配置文件
user nginx; worker_processes 2; error_log /log/nginx/error.log; #error_log /var/log/nginx/error.log notice; #error_log /var/log/nginx/error.log info; pid /run/nginx.pid; events { use epoll; # 采用epoll,是linux下最快的event worker_connections 2048; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; #gzip on; #反向代理配置 ,向内网6台jboss转发 upstream jboss { server 192.168.162.35:8080 weight=10; server 192.168.162.11:8080 weight=8; server 192.168.162.61:8080 weight=2; } server { listen 2011; server_name localhost; location ~ ^/nginx_status/ { stub_status on; access_log off; } location / { proxy_pass http://jboss; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
nginx location语法
基本语法:location [=|~|~*|^~] /uri/ { … }
= 严格匹配。如果这个查询匹配,那么将停止搜索并立即处理此请求。
~ 为区分大小写匹配(可用正则表达式)
!~为区分大小写不匹配
~* 为不区分大小写匹配(可用正则表达式)
!~*为不区分大小写不匹配
^~ 如果把这个前缀用于一个常规字符串,那么告诉nginx 如果路径匹配那么不测试正则表达式。
示例
=====
location = / {
# 只匹配 / 查询。
}
location / {
# 匹配任何查询,因为所有请求都已 / 开头。但是正则表达式规则和长的块规则将被优先和查询匹配。
}
location ^~ /p_w_picpaths/ {
# 匹配任何已 /p_w_picpaths/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。
}
location ~*.(gif|jpg|jpeg)$ {
# 匹配任何已 gif、jpg 或 jpeg 结尾的请求。
}
location ~*.(gif|jpg|swf)$ {
valid_referers none blocked start.igrow.cn sta.igrow.cn;
if ($invalid_referer) {
#防盗链
rewrite ^/ http://$host/logo.png;
}
}