前言:
nginx官网下载地址:
地址一:http://nginx.org/en/download.html 地址二:http://nginx.org/download/
以上两个地址都是nginx官网提供的下载地址,可根据实际情况下载自己需要的版本。目前最高稳定版本为 nginx-1.19.1.tar.gz
正文:
一、在线安装
1、环境准备
创建一个新目录,并安装编译工具及库文件。
[root@www ~]# mkdir -p /usr/local/env/nginx # -p表示循环创建多级目录
[root@www ~]# cd /usr/local/env/nginx
[root@www nginx]# yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
对于上述各种包的安装,笔者进行简单的介绍,有助理解:
- make
通常情况下,这个操作都是可以省略的,因为Linux系统基本都自带make包,所以不需要额外下载安装,但是安装了也没关系,如果系统检测到已经安装,就会跳过 make 的安装步骤。 - zlib-devel
nginx依赖的解压包。 - gcc-c++
nginx基于gcc编译环境,所以首先要下载gcc的编译环境。 - libtool 通用库
libtool 通用库包含了gcc、zlib-devel、openssl-devel等,是一个比较综合的文件库,libtool跟上面下载的其他包会有交集,就是因为libtool包含了一部分之前下载的rpm包,同样的,有重复的包也只会下载安装一次。 - openssl-devel
ssl 功能需要 opensl 库,安装 openssl 和 openssl-devel。通常系统自带openssl,这里我们重点是下载 openssl-devel。
2、安装 pcre
nginx的http模块需要使用pcre来解析正则表达式,需要安装下载pcre。如今Web服务器基本都会用到重定向,pcre就是让nginx支持rewrite功能,实现Url的重写,达到重定向的目的。
2.1、下载 PCRE 安装包,下载地址:
http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
[root@www nginx]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
2.2、解压安装包
[root@www nginx]# tar -zxvf pcre-8.35.tar.gz
2.3、进入安装包目录
[root@www nginx]# cd pcre-8.35
2.4、编译安装
[root@www pcre-8.35]# ./configure --prefix=/usr/local/pcre-8.35
[root@www pcre-8.35]# make && make install
- –prefix:表示指定安装目录,这样的好处就是所有相关的配置文件都会在
/usr/local/pcre-8.35
目录下,方便管理
2.5、查看pcre版本
[root@www pcre-8.35]# pcre-config --version
3、安装 nginx
笔者以 nginx-1.19.1.tar.gz 为例
3.1、下载 Nginx,下载地址:
http://nginx.org/download/nginx-1.19.1.tar.gz
[root@www pcre-8.35]# cd ../
[root@www nginx]# wget http://nginx.org/download/nginx-1.19.1.tar.gz
3.2、解压安装包
[root@www nginx]# tar -zxvf nginx-1.19.1.tar.gz
3.3、进入安装包目录
[root@www nginx]# cd nginx-1.19.1
3.4、编译安装
[root@www nginx-1.19.1]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/env/nginx/pcre-8.35
[root@www nginx-1.19.1]# make && make install
注:
- –prefix:表示指定安装的目录,这样安装的文件就都在
/usr/local/nginx
目录下面,便于管理 - –with-pcre:后面的目录不能是 pcre 安装的目录,一定要是 pcre 的文件目录才可以,否则有些文件就找不到
3.5、进入nginx的安装目录
[root@www nginx-1.19.1]# cd /usr/local/nginx
查看nginx版本
[root@www nginx]# sbin/nginx -v
3.6、配置 nginx
创建nginx运行使用的用户
[root@www nginx]# /usr/sbin/groupadd nginx
[root@www nginx]# /usr/sbin/useradd -g nginx nginx
配置 nginx.conf :
[root@www nginx]# vim conf/nginx.conf
填写如下内容:
user nginx nginx;
worker_processes 4; #设置值和CPU核心数一致
error_log logs/error.log info; #日志位置和日志级别
pid logs/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
#配置Nginx worker进程最大打开文件数
worker_rlimit_nofile 65535;
events {
use epoll; #支持大量连接和非活动连接
worker_connections 65535; #最大连接数
multi_accept on; #nginx在已经得到一个新连接的通知时,接收尽可能多的连接,默认off
# accept_mutex 为on的好处是,能减少QPS(每秒查询率),但是会造成worker的压力不均衡,部分worker的CPU利用率很高达到98%,而部分worker的CPU利用率却较低的情况
# accept_mutex 为off,各个worker的CPU压力差别不大,但是会使QPS明显增加
accept_mutex on; #设置一个进程是否同时接受多个网络连接,防止惊群现象发生,1.11.3版本之前默认为on
}
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;
#charset gb2312;
#设置server块中的server_name属性的长度,默认很短,当域名过长时会报错,所以需要根据实际需求设置域名大小,一般设置1024就足够了
server_names_hash_bucket_size 1024;
#根据实际需要设置请求体的大小,如果请求中有上传文件这种需求,则需要把下面请求体相关参数调大以适应文件的大小
#以下参数配置可以放在http块中,则对全局有效;可以放在server块中,则对某个域名有效;也可以放在location块中,则对当前的映射路径有效。
client_header_buffer_size 1024k;
large_client_header_buffers 4 2048k;
client_max_body_size 8m;
client_body_timeout 1800; #单位是秒
sendfile on;
keepalive_timeout 60; #连接超时时间/秒
tcp_nopush on;
tcp_nodelay on;
# fastcgi_connect_timeout 300;
# fastcgi_send_timeout 300;
# fastcgi_read_timeout 300;
# fastcgi_buffer_size 64k;
# fastcgi_buffers 4 64k;
# fastcgi_busy_buffers_size 128k;
# fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
# 负载均衡模块示例:
# 根据权重分配,权重越大,分配的概率越大
upstream testUrl {
server 192.168.137.10:80 weight=75;
server 192.168.137.11:80 weight=50;
server 192.168.137.12:80 backup; # 备用服务。主机宕机或者忙的时候,请求backup机器。
}
# 每个请求根据访问IP的hash结果,并结合权重进行分配。ip_hash可以和weight配合使用
upstream test01 {
ip_hash;
server 192.168.137.10:8080 weight=2;
server 192.168.137.11:8080 weight=1;
server 192.168.137.12:8080 weight=1;
}
# 将请求分配到连接数最少的服务器上
upstream test02 {
least_conn;
server 192.168.137.10:8081 weight=2;
server 192.168.137.11:8082 weight=1;
server 192.168.137.12:8083 weight=1;
}
#下面是server虚拟主机的配置
server {
listen 80; #监听端口
server_name localhost; #域名
# root html; #站点目录为 /usr/local/nginx/html
# index index.html index.htm index.jsp; #默认打开的文件
location / {
root html; # 默认根目录为nginx安装目录下的html目录。可根据需要进行修改
index index.html index.htm;
}
# 负载均衡测试
location /test {
# 当地址栏输入 http://localhost/test 时,会根据 paoxy_pass 指定的地址中 testUrl 去 upstream 块中找名字相同的upstream块
proxy_pass http://testUrl/;
}
location /demo {
proxy_connect_timeout 120;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.137.10:1048/;
}
# 根据条件选择性转发
location /demo2 {
proxy_connect_timeout 120;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 100m;
set $pre_env "0";
if ($remote_addr ~* "^10\.168\.189\.2(4\d|5[0-5])$") { set $pre_env "1"; }
if ($remote_addr = "192.168.10.101") { set $pre_env "1"; }
if ($remote_addr = "192.168.124.10") { set $pre_env "1"; }
if ($remote_addr ~* "^124\.168\.47\.[1-225]$") { set $pre_env "1"; }
if ($pre_env = "1") { proxy_pass http://test01; } # 转发到test01对应的IP上
if ($pre_env != "1") { proxy_pass http://test02; } # 转发到test02对应的IP上
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# location ~ .*\.(php|php5)?$ {
# #fastcgi_pass unix:/tmp/php-cgi.sock;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# include fastcgi.conf;
# }
# location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$ {
# expires 30d;
# # access_log off;
# }
# location ~ .*\.(js|css)?$ {
# expires 15d;
# # access_log off;
# }
# access_log off;
}
}
检查配置文件的正确性
[root@www nginx]# ./sbin/nginx -t
显示 nginx.conf test is successful
表示配置正确
3.7、启停 nginx
[root@www nginx]# ./sbin/nginx # 启动 nginx
[root@www nginx]# ./sbin/nginx -s reload # 重新载入配置文件
[root@www nginx]# ./sbin/nginx -s reopen # 重新打开日志文件
[root@www nginx]# ./sbin/nginx -s stop # 快速停止nginx,不保存相关信息,迅速终止web服务
[root@www nginx]# ./sbin/nginx -s quit # 平稳关闭nginx,保存相关信息,有安排的结束web服务
–end–