Nginx源代码安装
标签(空格分隔): nginx
root用户执行安装组件
# rpm -q gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre-devel
# yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre-devel
普通应用账号安装Nginx
准备组件
这两个组件是github上,下载下来并解压
- 缓存净化组件:ngx_cache_purge
- 后端健康检查组件nginx_upstream_check_module
$ tar -xzvf nginx-1.11.6.tar.gz
$ cd nginx-1.11.6
# 这句参考[README](https://github.com/yaoweibin/nginx_upstream_check_module) 安装补丁,后面的路径是下载下来的组件的解压地址:
$ patch -p1 < /home/maip/soft/nginx/nginx_upstream_check_module-master/check_1.11.5+.patch
参考README 安装补丁,依次输入:
- File to patch: src/http/modules/ngx_http_upstream_hash_module.c
- File to patch: src/http/modules/ngx_http_upstream_ip_hash_module.c
- File to patch: src/http/modules/ngx_http_upstream_least_conn_module.c
- File to patch: src/http/ngx_http_upstream_round_robin.c
- File to patch: src/http/ngx_http_upstream_round_robin.h
编译安装
# 这条命令里面的几个参数路径根据具体情况修改
$ ./configure --prefix=/home/maip/libs/nginx --conf-path=/home/maip/libs/nginx/nginx.conf --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --add-module=/home/maip/soft/nginx/ngx_cache_purge-master --add-module=/home/maip/soft/nginx/nginx_upstream_check_module-master
$ make && make install
- 创建Nginx缓存目录
$ mkdir -p /home/maip/libs/nginx/cache_files
- 创建日志目录
$ mkdir -p /home/maip/libs/nginx/logs/
- 创建进程运行目录
mkdir -p /home/maipusr/libs/nginx/run
- 修改配置
vim /home/maip/libs/nginx/nginx.conf
#运行用户
#user nobody;
#==worker进程数,通常设置等同于CPU数量,auto为自动检测
worker_processes auto;
#==worker进程打开最大文件数,可CPU*10000设置
#worker_rlimit_nofile 20000;
#全局错误日志
error_log logs/error.log;
pid run/nginx.pid;
events {
#==worker进程同时打开的最大连接数,可CPU*2048设置
worker_connections 2048;
#==告诉nginx收到一个新链接通知后接受尽可能多的链接
multi_accept on;
#==设置用于复用客户端线程的轮训方法
use epoll;
}
http {
#==优化磁盘IO设置,指定nginx是否调用sendfile函数来输出文件,普通应用设为on,下载等磁盘IO高的应用,可设为off
sendfile on;
#==给客户端分配keep-alive链接超时时间
keepalive_timeout 30;
include mime.types;
default_type application/octet-stream;
#default_type text/html;
#设置默认字符集
charset UTF-8;
#==设置nginx采用gzip压缩的形式发送数据,减少发送数据量,但会增加请求处理时间及CPU处理时间,需要权衡
gzip on;
#允许或禁止压缩基于请求和相应的响应流,any代表压缩所有请求
gzip_proxied any;
#==设置对数据启用压缩的最少字节数,如果请求小于10240字节则不压缩,会影响请求速度
gzip_min_length 10240;
#==设置数据压缩等级,1-9之间,9最慢压缩比最大
gzip_comp_level 2;
#设置需要压缩的数据格式
gzip_types text/plain text/css text/xml text/javascript application/json application/x-javascript application/xml application/xml+rss;
#==开发缓存的同时也指定了缓存文件的最大数量,20s如果文件没有请求则删除缓存
open_file_cache max=100000 inactive=20s;
#==指多长时间检查一次缓存的有效信息
open_file_cache_valid 60s;
#==文件缓存最小的访问次数,只有访问超过5次的才会被缓存
open_file_cache_min_uses 5;
#当搜索一个文件时是否缓存错误信息
open_file_cache_errors on;
#==允许客户端请求的最大单文件字节数
client_max_body_size 8m;
#==冲区代理缓冲用户端请求的最大字节数
client_header_buffer_size 32k;
port_in_redirect off;
#proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
#==nginx跟后端服务器连接超时时间(代理连接超时)
proxy_connect_timeout 60;
#==连接成功后,后端服务器响应时间(代理接收超时)
proxy_read_timeout 120;
#==后端服务器数据回传时间(代理发送超时)
proxy_send_timeout 20;
#==设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffer_size 32k;
#==proxy_buffers缓冲区,网页平均在32k以下的设置
proxy_buffers 4 128k;
#==高负荷下缓冲大小(proxy_buffers*2)
proxy_busy_buffers_size 256k;
#==设定缓存文件夹大小,大于这个值,将从upstream服务器传
proxy_temp_file_write_size 256k;
log_format main '$status $request_time $upstream_response_time $body_bytes_sent $remote_addr $remote_user [$time_local] $host "$request" "$http_referer" "$http_user_agent" "$http_x_forwarded_for" $upstream_addr $upstream_cache_status "in: $http_cookie"';
access_log logs/access.log main;
#==1G内存缓冲空间,3天不用删除,最大磁盘缓冲空间2G
proxy_cache_path /home/maip/libs/nginx/cache_files levels=1:2 keys_zone=cache_one:1024m inactive=3d max_size=2g;
include "vhosts/*.conf";
}
- 创建项目配置文件
$ mkdir vhosts
$ touch vhosts/maip.conf
$ vim vhosts/maip.conf
upstream upstream_app_servers {
server 10.92.82.34:8811 weight=1 max_fails=3 fail_timeout=5s;
}
server {
listen 8088;
server_name localhost,10.92.82.34;
#用于清除缓存
#原缓存URL地址:http://test.easyshow.zzsoon.com/easyshow/iconfont/iconfont.css
#清理缓存URL地址:http://test.easyshow.zzsoon.com/purge/easyshow/iconfont/iconfont.css
location ~ /purge(/.*) {
#设置只允许指定的IP或IP段才可以清除URL缓存。
allow all;
#deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
location ~ /maip/action(/.*) {
add_header Cache-Control 'no-store';
proxy_pass http://upstream_app_servers;
}
location ~ /maip(/.*) {
proxy_cache cache_one;
add_header Nginx-Cache $upstream_cache_status;
proxy_cache_valid 200 304 301 302 8h;
proxy_cache_valid 404 1m;
proxy_cache_valid any 2d;
proxy_cache_key $host$uri$is_args$args;
proxy_pass http://upstream_app_servers;
expires 30d;
}
# 需安装FastDFS插件
location ~/group([0-9])/M00 {
root /home/maip/libs/fastdfs/storage/data/;
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#禁止访问的文件.htxxx
location ~ /\.ht {
deny all;
}
}
- 配置环境变量
$ vim ~/.bash_profile
新增
export NGINX_HOME=/home/maip/libs/nginx
$ source ~/.bash_profile
查看nginx安装情况
$ /home/maip/libs/nginx/sbin/nginx -V
启动nginx
$ /home/maip/libs/nginx/sbin/nginx
停止ngnix
$ /home/maip/libs/nginx/sbin/nginx -s stop
$ for sid in `ps -ef | grep nginx | awk '{print $2}'`; do kill -9 $sid; done
重新加载ngnix配置
$ /home/maip/libs/nginx/sbin/nginx -s reload