nginx编译安装
源码编译安装nginx
1、安装pcre软件包(使nginx支持http rewrite模块)
[root@localhost]# yum install -y pcre [root@localhost]# yum install -y pcre-devel
2、安装openssl-devel(使nginx支持ssl)
[root@localhost]# yum install -y openssl-devel
3、创建用户nginx
[root@localhost]# useradd nginx [root@localhost]# passwd nginx
4、安装nginx
[root@localhost ~]tar -vzxf nginx-1.11.3.tar.gz -C /usr/local [root@localhost ~]cd nginx-1.11.3/ [root@localhost nginx-1.11.3]# ./configure --prefix=/web/soft/nginx [root@localhost nginx-1.11.3]# make && make install
5、修改配置文件/web/soft/nginx/conf/nginx.conf
#全局参数设置 worker_processes 1; #设置nginx启动进程的数量,一般设置成与逻辑cpu数量相同 error_log logs/error.log; #指定错误日志 worker_rlimit_nofile 102400; #设置一个nginx进程能打开的最大文件数 pid /var/run/nginx.pid; events { worker_connections 1024; #设置一个进程的最大并发连接数 } #http服务相关设置 http { include mime.types; 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 /var/log/nginx/access.log main; #设置访问日志的位置和格式 sendfile on; #是否调用sendfile函数输出文件,一般设置为on,若nginx是用来进行磁盘IO负载应用时,可以设置为off,降低系统负载 gzip on; #是否开启gzip压缩 keepalive_timeout 65; #设置长连接的超时时间 #虚拟服务器的相关设置 server { listen 80; #设置监听的端口 server_name localhost; #设置绑定的主机名、域名或ip地址charset koi8-r; #设置编码字符 location / { root /var/www/nginx; #设置服务器默认网站的根目录位置 index index.html index.htm; #设置默认打开的文档 } error_page 500 502 503 504 /50x.html; #设置错误信息返回页面 location = /50x.html { root html; #这里的绝对位置是/var/www/nginx/html } } }
6、检测nginx配置文件是否正确
[root@localhost]# nginx -t
7、启动nginx服务nginx8、通过nginx -s控制nginx服务
[root@localhost]# nginx #启动服务 [root@localhost]# nginx -s stop #停止服务 [root@localhost]# nginx -s quit #退出服务 [root@localhost]# nginx -s reopen #重新打开日志文件 [root@localhost]# nginx -s reload #重新加载配置文件
9、实现nginx开机自启
[root@localhost]# vim /etc/init.d/nginx-80 #!/bin/bash # nginx Startup script for the Nginx HTTP Server # this script create it by caffreyxin at 2007.10.15. # it is v.0.0.1 version. # if you find any errors on this scripts, please contact caffreyxin. # and send mail to xinyflove at sina dot com. # # 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 n_path="${nginx_path}" nginxd=${n_path}/sbin/nginx nginx_config=${n_path}/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: " 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
添加权限
[root@localhost]# chmod +x /etc/init.d/nginx
设置开机自启
[root@localhost]# chkconfig nginx on 10
nginx日志文件详解
nginx日志文件分为log_format和access_log两部分log_format定义记录的格式,其语法格式为
log_format 样式名称 样式详情
配置文件中默认有
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; 变量说明$remote_addr和$http_x_forwarded_for客户端的ip$remote_user客户端的名称$time_local访问时的本地时间$request请求的URL和http协议$status访问的状态码$body_bytes_sent发送给客户端的主体内容大小$http_referer记录客户端是从哪个页面链接访问过来的,若没有链接,则访问‘-’$http_user_agent记录客户端使用的浏览器的相关信息 access_log主要指定使用哪种格式记录和日志文件的位置,其语法格式为 access_log 日志文件路径 样式名称如:access_log /var/log/nginx/access.log main;下面是日志内容的