dream_653
作者dream_6532019-11-26 10:03
系统应用运维, *****

Linux 系统安装配置 Nginx

字数 5097阅读 4279评论 0赞 3

1. 前言

Nginx 是一款轻量级的代理服务器,其特点是占有内存少,并发能力强,在集群方案中作为负载均衡使用。

nginx-1.14.1 之前的版本存在多个安全问题,推荐使用 nginx-1.15.6,历史版本用户建议进行升级.

2. 环境准备

必备环境

安装Nginx必须有gcc编译器,如果没有需要自行安装。
检查命令: gcc -v
安装包
nginx-1.15.6.tar.gz
依赖包
必须下载3个依赖包,部署过程会用到。
zlib-1.2.11.tar.xz
pcre-8.42.tar.bz2
openssl-1.1.1c.tar.gz
补丁包
建议下载,主动健康检查补丁包为重要功能。
ngx_healthcheck_module-master.zip
wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master

3. 安装过程

3.1 解压安装包

将安装包放在某个目录,例如 /usr/nginx 下,进行解压

 mkdir /usr/nginx # 创建安装目录  
cd /usr/nginx # 进入目录  
tar zxvf nginx-1.15.6.tar.gz # 解压 nginx-1.15.6  
tar jxvf pcre-8.42.tar.bz2 # 解压 pcre  
tar jxvf zlib-1.2.11.tar.xz # 解压 zlib  
tar zxvf openssl-1.1.1c.tar.gz # 解压 openssl  
unzip ngx_healthcheck_module-master.zip # 解压 ngx_healthcheck_module-master

3.2 安装 Nginx

cd /usr/nginx/nginx-1.15.6 # 进入解压目录  
patch -p1 < /usr/nginx/ngx_healthcheck_module-master/nginx_healthcheck_for_nginx_1.14+.patch # 应用健康检查补丁  
./configure --prefix=/usr/nginx --with-pcre=/usr/nginx/pcre-8.42 --with-zlib=/usr/nginx/zlib-1.2.11 --with-openssl=/usr/nginx/openssl-1.1.1c --with-stream --add-module=/usr/nginx/ngx_healthcheck_module-master # 对即将安装的软件进行配置,检查当前的环境是否满足要安装软件./configure --prefix=/的依赖关系  
make && make install # 用 && 连接的两条命令,只有 make 无错误时,才会继续执行 make install 命令

4. 修改配置

要想使用 Nginx 搭配 Web 容器发挥负载均衡的作用,必须还要对 Nginx 进行配置,下面提供通用配置和自定义配置。

注意:修改配置后,需要重启 Nginx 方可生效。

4.1 通用配置

通用配置 :nginx-通用版.conf

#user root;  
worker_processes auto;  
#worker_cpu_affinity auto;  
  
error_log logs/error.log;  
#error_log logs/error.log notice;  
#error_log logs/error.log info;  
#pid logs/nginx.pid;  
  
events {  
worker_connections 1024;  
}  
  
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" $upstream_addr';   
access_log off;  
#access_log logs/access.log main;  
sendfile on;  
#tcp_nopush on;  
#keepalive_timeout 0;  
keepalive_timeout 65s;  
types_hash_max_size 2048;  
#gzip on;  
#gzip_disable "MSIE [1-6].";  
client_header_buffer_size 512k;  
large_client_header_buffers 4 512k;  
client_max_body_size 100M;  
  
upstream FR.com {  
server 192.168.1.1:9999 max_fails=15 fail_timeout=300s;  
server 192.168.1.2:9999 max_fails=15 fail_timeout=300s;  
  
#<---------------ngx_healthcheck_module--------------->#  
check interval=2000 rise=5 fall=10 timeout=10000 type=http;  
check_http_send "GET /WebReport/decision/system/health HTTP/1.0\\r\\n\\r\\n";  
check_http_expect_alive http_2xx http_3xx;  
#<---------------ngx_healthcheck_module--------------->#  
}  
  
upstream WBS.com {   
server 192.168.1.1:38888 max_fails=15 fail_timeout=300s;  
server 192.168.1.2:38888 max_fails=15 fail_timeout=300s;  
ip_hash;  
}  
server {  
listen 8989;  
server_name 192.168.1.1;  
underscores_in_headers on;  
#charset koi8-r;  
#access_log logs/host.access.log main;  
  
#<---------------Nginx upstream status monitor--------------->#  
#location /status {  
# healthcheck_status html;  
#}  
#<---------------Nginx upstream status monitor--------------->#  
location /nstatus {  
check_status;  
access_log off;  
#allow SOME.IP.ADD.RESS;  
# #deny all;  
}  
location / {  
proxy_http_version 1.1;  
proxy_pass http://FR.com;  
proxy_next_upstream http_500 http_502 http_503 http_504 http_403 http_404 http_429 error timeout invalid_header non_idempotent;  
#proxy_next_upstream_timeout 0;  
#proxy_next_upstream_tries 0;  
proxy_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_set_header Connection "";  
#proxy_buffering off;  
#proxy_buffer_size 64k;  
#proxy_buffers 32 64k;  
#proxy_busy_buffers_size 64k;  
  
proxy_connect_timeout 75;  
proxy_read_timeout 400;  
proxy_send_timeout 400;  
}  
  
#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;  
}  
}  
server {   
#FineReport:38889 FineBI:48889  
#listen 38889;  
listen 48889;  
server_name 192.168.1.1;  
location / {   
proxy_http_version 1.1;  
proxy_pass http://WBS.com;  
proxy_connect_timeout 75;  
proxy_read_timeout 400;  
proxy_send_timeout 400;  
  
proxy_set_header Upgrade $http_upgrade;  
proxy_set_header Connection "upgrade";  
}  
}  
}

然后替换原始的 /usr/nginx/conf/nginx.conf

4.2 自定义配置

vi /usr/nginx/conf/nginx.conf # 编辑配置文件 nginx.conf

参考 nginx.conf 配置手册 中的 Nginx 配置参数说明,自行配置(修改 Nginx 端口、配置外网映射、配置 https 等)。

5. 启动Nginx

5.1 运维命令

cd /usr/nginx/sbin # 进入 sbin 目录  

./nginx # 启动 nginx
./nginx -s stop # 停止 nginx
./nginx -s reload # 热加载nginx,可以理解为重启,但是用户不会感知到

5.2 测试是否成功

在浏览器输入 ip:负载均衡端口号/status 查看健康页面,例如:192.168..1.1:8989/nstatus

,可以看到节点健康状态,出现这个页面说明Nginx 安装成功。

6. FAQ 文档

6.1 缺少 patch命令

解决方法:离线安装 patch 命令。

1)将patch源码包传至 usr 目录下:patch-2.7.6.tar.gz

2)解压并对其进行编译安装,即可使用patch命令

tar zxvf patch-2.7.6.tar.gz  # 解压安装包  
cd usr/patch-2.7.6  # 进入到指定目录下  
./configure && make && make install  # 编译并安装

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

3

添加新评论0 条评论

Ctrl+Enter 发表

作者其他文章

相关文章

相关问题

相关资料

X社区推广