前期准备
Nginx及安装过程中需要的依赖程序的下载链接参考如下:
1.Nginx,参考下载链接(建议下载stable version):http://nginx.org/en/download.html
2.nginx-sticky-module,参考下载链接:https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/downloads/
3.pcre,参考下载链接:https://ftp.pcre.org/pub/pcre/
4.openssl,参考下载链接:https://www.openssl.org/source/
5.zlib,参考下载链接:https://zlib.net/
若系统未安装gcc,请使用操作系统安装盘(或安装镜像)进行安装
安装Nginx
以所有的安装程序位于/apps/softwore/Nginx下,nginx的安装目录位于/apps/nginx下,依次执行以下命令进行nginx的安装(命令中标红的安装包名称,以实际下载的为准):
#切换目录至/apps/softwore/Nginx
cd /apps/softwore/Nginx
# 以实际下载的文件格式选择tar或zip解压
# 解压tar压缩包
tar -zxvf nginx-1.20.1.tar.gz
tar -zxvf pcre-8.40.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
tar -zxvf openssl-1.1.0g.tar.gz
# 解压zip压缩包
unzip nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip
# 修改解压后文件夹名,便于后续安装
mv nginx-1.20.1 nginx
mv pcre-8.40 pcre
mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42 nginx-sticky-module
mv zlib-1.2.11 zlib
mv openssl-1.1.0g openssl
# 赋权文件夹
chmod -R 777 nginx
chmod -R 777 openssl
chmod -R 777 zlib
chmod -R 777 nginx-stick-module
chmod -R 777 pcre
# 进行nginx
cd nginx
# 注意以下configure命令为一行
./configure --prefix=/apps/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-http_random_index_module --with-http_gzip_static_module --with-pcre=/apps/softwore/Nginx/pcre --add-module=/apps/softwore/Nginx/nginx-sticky-module --with-openssl=/apps/softwore/Nginx/openssl --with-zlib=/apps/softwore/Nginx/zlib
# 编译、安装
make
make install
配置Nginx
以nginx安装在/apps/nginx下为例,nginx的配置文件为/apps/nginx/conf/nginx.conf。配置文件内容示例如下(其中upstream的名称及服务地址依据实际情况进行修改):
worker_processes auto;
worker_rlimit_nofile 20960;
error_log logs/error.log crit;
events {
worker_connections 4096;
multi_accept on;
accept_mutex on;
accept_mutex_delay 500ms;
}
http {
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
access_log off;
include mime.types;
default_type application/octet-stream;
keepalive_timeout 300;
client_max_body_size 10240M;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 3;
gzip_types text/xml text/plain text/css text/javascript application/x-javascript application/javascript application/xml;
gzip_disable "MSIE [1-6]\.";
upstream 192.168.31.142{
sticky;
server 192.168.31.157:80 max_fails=5 fail_timeout=20s; # 代理前服务器
# server 192.168.0.2:80 max_fails=5 fail_timeout=20s;
}
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
proxy_pass http://192.168.31.142; # 此处设置为外网访问地址
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
proxy_connect_timeout 300;
proxy_read_timeout 300;
proxy_send_timeout 300;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
启动Nginx
以nginx安装在/apps/nginx下为例,nginx的启动脚本为/apps/nginx/sbin/路径下的:nginx文件。启动示例如下:
# 切换命令行到nginx启动脚本目录
cd /apps/nginx/sbin
# 启动
./nginx
# 测试连接
./nginx -t
# 重启
./nginx –s reload
# 指定配置文件重启,一般用于nginx异常停止后的启动
./nginx –c /apps/nginx/conf/nginx.conf
启动nginx时,报端口被占用的错
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
修改方式:
fuser -k 80/tcp
将进程杀死后,启动nginx。