安装及运行
1.编译安装Nginx
Nginx的运行及配置需要pcre、zlib的开发包,以便提供相应的库和头文件
yum -y install pcre-devel zlib-devel
2.创建运行用户、组
useradd -M -s /sbin/nologin nginx
3.编译安装Nginx
tar xzf nginx-*.tar.gz -C /usr/src
cd /usr/src/nginx-*
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
make && make insatll
http_stub_status_module
模块以支持状态统计,便于查看服务器的连接信息
4.优化路径和控制服务
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
killall -s HUP nginx //重载服务
killall -s QUIT nginx //关闭服务
配置文件
1.全局配置
#user nobody; //运行用户 默认为nobody
worker_processes 1; //工作的进程数量,可以根据cpu核心总数来指定工作进程数
#error_log logs/error.log; //错误日志文件的位置
#pid logs/nginx.pid; //PID文件的位置`
2.I/O事件配置
events {
use epoll //使用epoll模型
worker_connections 4096; //每个进程处理4096个进程连接
}
3.HTTP配置
http {
include mime.types;
default_type application/octet-stream;
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 main; //去掉前面的#,访问日志位置
sendfile on; //去掉前面的#,支持文件发送(下载)
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65; //连接保持超时
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location / status{ //访问位置为/status
stub_status on; //打开状态统计功能
access_log off; //关闭此位置的日志记录
}
#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;
}
}
配置完后访问http://网站/status可以查看访问状态统计