一:这节主要讲解gzip配置,status配置,虚拟主机配置
gzip配置
- gzip on; 启用或禁用压缩响应
- gzip_min_length 1K; 设置gzip响应的最小长度,这个长度取决于Content-length字段
- gzip_buffers 4 16k; 设置多少4个大小为16K的buffer用于压缩响应,默认为内存页的大小4K或者是8K取决于平台
- gzip_http_version 1.1; 压缩响应的http版本
- gzip_comp_level 2; 压缩的级别
- gzip_types text/css text/plain application/x-javascript application/xml; 压缩的类型
- gzip_vary; 启用应答头“Vary: Accept-Encoding”
status配置
- location /nginx { #定义通过访问http://localhost/nginx进入status界面
- stub_status on; #开启stub_status功能
- access_log off;
- auth_basic "administrator login"; #定义弹出的登陆框的提示信息
- auth_basic_user_file /usr/local/nginx/htpasswd; #定义登陆的账号和密码信息
- }
- 密码文件的创建需要借助apache自带的工具htpasswd来创建 没有htpasswd可以yum安装apache
- htpasswd -c /usr/local/nginx/htpasswd admin 回车后会要求输入密码
- 到此配置完成
虚拟主机配置
- 虚拟主机的配置主要有基于域名的 基于端口的 基于IP的 这里我介绍下基于域名和和基于IP的
- 基于IP的用的不多 毕竟IP资源比较匮乏主要是利用ifconfig的别名技术在eth0网卡上再加上一个IP
- ifconfig eth0:0 10.17.0.222 netmask 255.255.255.0
- 然后通过listen让其监听再IP:80的套接字上,而不是直接listen 80让其监听再*:80上 目的是让nginx识别出是请求哪个server主机的
- server {
- listen 10.17.0.222:80;
- server_name 10.17.0.222;
- location / {
- root /var/www/html;
- index index.html index.htm;
- }
- }
- server {
- listen 10.17.0.27:80;
- server_name 10.17.0.27;
- location / {
- root /var/www/html;
- index index.html index.htm;
- }
- }
- 基于域名的比较简单修改server_name不同即可,因为我们无法搞到域名,我们可以再客户机上通过c:/windows/system32/drivers/etc/host文件加入
- 10.17.0.222 www.test1.com让其通过hosts文件来解析
- server {
- listen 80;
- server_name www.test1.com;
- location / {
- root /var/www/html;
- index index.html index.htm;
- }
- }
- server {
- listen 80;
- server_name www.test2.com;
- location / {
- root /var/www/html;
- index index.html index.htm;
- }
- }
- 当虚拟主机非常多的时候我们可以用innclude来包含一个文件或者一个目录下的所有文件 这样让nginx的配置文件更容易查看
二:nginx的启动脚本和日志
通过下面脚本将nginx做成服务形式
- #!/bin/sh
- #
- # nginx - this script starts and stops the nginx daemon
- #
- # 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/local/nginx/sbin/nginx"
- prog=$(basename $nginx)
- NGINX_CONF_FILE="/usr/local/nginx/conf/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 $"Starting $prog: "
- daemon $nginx -c $NGINX_CONF_FILE
- retval=$?
- echo
- [ $retval -eq 0 ] && touch $lockfile
- return $retval
- }
- stop() {
- echo -n $"Stopping $prog: "
- killproc $prog -QUIT
- retval=$?
- echo
- [ $retval -eq 0 ] && rm -f $lockfile
- return $retval
- killall -9 nginx
- }
- restart() {
- configtest || return $?
- stop
- sleep 1
- start
- }
- reload() {
- configtest || return $?
- echo -n $"Reloading $prog: "
- killproc $nginx -HUP
- RETVAL=$?
- echo
- }
- force_reload() {
- restart
- }
- configtest() {
- $nginx -t -c $NGINX_CONF_FILE
- }
- rh_status() {
- status $prog
- }
- rh_status_q() {
- rh_status >/dev/null 2>&1
- }
- case "$1" in
- start)
- rh_status_q && exit 0
- $1
- ;;
- stop)
- rh_status_q || exit 0
- $1
- ;;
- restart|configtest)
- $1
- ;;
- reload)
- rh_status_q || exit 7
- $1
- ;;
- force-reload)
- force_reload
- ;;
- status)
- rh_status
- ;;
- condrestart|try-restart)
- rh_status_q || exit 0
- ;;
- *)
- echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
- exit 2
- esac
日志轮替
将该脚本放入crontab定时任务中每天进行日志轮替
- #!/bin/sh
- log_dir="/usr/local/nginx/logs/"
- yesterday=`date +%Y%m%d -d '-1 day'`
- lastday =`date +%Y%m%d -d '-1 month'`
- /bin/rm ${log_dir}/access.${lastday}.log
- /bin/rm ${log_dir}/error.${lastday}.log
- /bin/mv ${log_dir}/access.log ${log_dir}/access.${yesterday}.log
- /bin/mv ${log_dir}/error.log ${log_dir}/error.${yesterday}.log
- kill -USR1 `cat /var/run/nginx.pid`
- /bin/gzip ${log_dir}/access.${yesterday}.log &
- /bin/gzip ${log_dir}/error.${yesterday}.log &
三 nginx的信号控制
- nginx can be controlled with signals. The process ID of the master process is written to the file /usr/local/nginx/logs/nginx.pid by default. This name may be changed at configuration time, or in nginx.conf using the pid directive. The master process supports the following signals:
- TERM, INT fast shutdown TERM信号快速关闭
- QUIT graceful shutdown 完美的关闭
- #重新读取配置文件并检测语法是否正确,若正确则加载配置文件重新启动nginx的master进程
- HUP changing configuration, keeping up with a changed time zone (only for FreeBSD and Linux), starting new worker processes with a new configuration, graceful shutdown of old worker processes
- #重新打开log文件
- USR1 re-opening log files
- #升级可执行文件 用户热部署
- USR2 upgrading an executable file
- WINCH graceful shutdown of worker processes