在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
一、准备 #追加一个www组 groupadd -f www #追加一个nginx用户 useradd -s /sbin/nologin -g www nginx
#建立nginx日志目录 mkdir /var/log/nginx #赋予访问权限 chown nginx.www /var/log/nginx
./configure --prefix=/opt/servers/nginx \ --user=nginx \ --group=www \ --pid-path=/var/run/nginx.pid \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-pcre=/opt/software/pcre-8.10 \ --with-zlib=/opt/software/zlib-1.2.5 \ --with-http_stub_status_module \ --with-http_realip_module \ --with-http_gzip_static_module \ --without-http_fastcgi_module \ --without-http_memcached_module \ --without-http_map_module \ --without-http_geo_module \ --without-http_autoindex_module \ --with-poll_module && make && make install
vim /etc/init.d/nginx
#!/bin/bash # v.0.0.1 # create by jackbillow at 2007.10.15 # nginx - This shell script takes care of starting and stopping nginx. # # chkconfig: - 60 50 # description: nginx [engine x] is light http web/proxy server # that answers incoming ftp service requests. # processname: nginx # config: /etc/nginx.conf nginx_path="/opt/servers/nginx" nginx_pid="/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 [ -x $nginx_path/sbin/nginx ] || exit 0 RETVAL=0 prog="nginx" start() { # Start daemons. if [ -e $nginx_pid -a ! -z $nginx_pid ];then echo "nginx already running...." exit 1 fi if [ -e $nginx_path/conf/nginx.conf ];then echo -n $"Starting $prog: " $nginx_path/sbin/nginx -c $nginx_path/conf/nginx.conf & RETVAL=$? [ $RETVAL -eq 0 ] && { touch /var/lock/subsys/$prog success $"$prog" } echo else RETVAL=1 fi return $RETVAL } # Stop daemons. stop() { echo -n $"Stopping $prog: " killproc -d 10 $nigx_path/sbin/nginx RETVAL=$? echo [ $RETVAL = 0 ] && rm -f $nginx_pid /var/lock/subsys/$prog } # See how we were called. case "$1" in start) start ;; stop) stop ;; restart) stop start ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $0 {start|stop|restart|status}" exit 1 esac exit $RETVAL
nginx_path="/opt/servers/nginx" nginx_pid="/var/run/nginx.pid"
chmod +x /etc/init.d/nginx
chkconfig --add nginx chkconfig nginx on
#启动nginx service nginx start #停止nginx service nginx stop #重启nginx service nginx restart #查看nginx状态 service nginx status
vim /opt/servers/nginx/conf/nginx.conf 微调 #使用的用户和组,这里我们为nginx服务新建了nginx账户和www工作组 user nginx www; #制定的工作衍生进程数(2倍于CPU内核数) worker_processes 4; #错误日志存放路径,日志级别由低到高[debug | info | notice | warn | error | crit] error_log /var/log/nginx/error.log crit; #指定文件描述符数量 与ulimit -n数值保持一致 work_rlimit_nofile 65535; events { #使用的网络I/O模型,Linux用epoll模型,Unix用kqueue模型 use epoll; #允许的连接数 worker_connections 51200; } http{ include mime.types; default_type application/octet-stream; #追加 '"$sent_http_cache_control" "$sent_http_pl" "$request_time"'获取请求细节信息 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"' '"$sent_http_cache_control""$sent_http_pl" "$request_time"'; access_log /var/log/nginx/access.log main; ... server{ ... location / { root html; index index.html index.htm index.jsp index.do; #在header中传递请求放host、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; proxy_pass_header Content-Type; proxy_pass_header Content-Disposition; proxy_pass_header Content-Length; ... } } }
location /image/ { root /data; } 当我们访问“/image/”路径时,实际上访问的是“/data/image/”,注意“/data”后面不要有“/” location /image/ { alias /data/img/; } 当我们访问“/image/”路径时,实际上访问的是“/data/img/”,注意“/data/img/”以“/”结尾。 六、重定向 rewrite ^/activity(.*)$ / last;
rewrite ^/activity(.*)$ /$1 last;
location /status { stub_status on; access_log off; allow 10.10.0.0/16; allow 10.1.0.0/16; allow 10.11.0.0/16; deny all; }
Active connections: 14 server accepts handled requests 62 62 302 Reading: 0 Writing: 3 Waiting: 11
#!/bin/bash # THis script run at 00:00 # author dongliang at 2012-09-07 # Nginx Log Path logs_path="/var/log/nginx/" # Nginx PID Path nginx_pid="/var/run/nginx.pid" mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/ mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m% d").log mv ${logs_path}error.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/error_$(date -d "yesterday" +"%Y%m%d" ).log kill -USR1 `cat $nginx_pid`
chmod +x nginx_log.sh 凌晨执行 crontab -e 0 0 * * * /opt/script/nginx_log.sh 八、Nginx负载均衡 upstream tomcat { server 10.11.155.26:8080; server 10.11.155.41:8080; } 接着修改location节点,配置代理: location / { ... proxy_pass http://tomcat; ... } 当访问根路径时,会轮播路由到两台服务器上,至于后端服务器是tomcat还是jetty之类的,都无所谓,照葫芦画瓢就是了。 例如,可以这样配置: upstream tomcat { server 10.11.155.26:8080 weight=5; server 10.11.155.41:8080 weight=10; } 后者分得的请求数就会较高。 |
请发表评论