1,nginx安装(目录为/usr/src/)
1,CentOS上传下载文件命令:
yum -y install lrzsz
2,安装依赖:pcre
1)首先去官网下载压缩包文件
其他的source网站需要墙,点击https://www.pcre.org/,我使用的ftp下载的
https://ftp.pcre.org/pub/pcre/pcre-8.37.tar.gz
https://ftp.pcre.org/pub/pcre/
yum安装:yum install -y pcre pcre-devel
# 检查各依赖是否安装
gcc -v
rpm -qa pcre
yum list installed | grep zlib*
rpm -qa openssl
# 安装依赖
yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-deve
yum install -y openssl openssl-devel
# 编译安装
# 获取安装包
wget https://ftp.pcre.org/pub/pcre/pcre-8.37.tar.gz
# 解压缩
tar -xzvf pcre-8.37.tar.gz
# 进入该目录
cd pcre-8.37
# 运行configure,后面的两个参数之间没有空格
./configure --enable-utf8
# 执行configure命令后如果缺少编译依赖,可根据提示安装
# 例如:configure: error: You need a C++ compiler for C++ support.
# yum install -y gcc gcc-c++
# 执行make命令
make && make install
# 查看是否安装成功
pcre-config --version
3,安装依赖:openssl,
yum install -y openssl openssl-devel
4,安装依赖:zlib
yum install -y zlib zlib-devel
5,最后安装:nginx
# 上传安装包到指定目录,解压
tar -xzvf nginx-1.20.1.tar.gz
# 切换到解压目录,执行
cd nginx-1.20.1
./configure
# 执行make命令
make && make install
安装后会打印出 nginx的安装目录 /usr/local/nginx 以及配置文件目录/usr/local/nginx/conf/nginx.conf
2,操作nginx
1,初次启动
cd /usr/local/nginx/sbin
执行指令: ./nginx
2,停止nginx
cd /usr/local/nginx/sbin
执行指令:./nginx -s stop
3,重启nginx,重新加载配置文件
cd /usr/local/nginx/sbin
执行指令:./nginx -s reload
4,查看nginx是够启动
ps -ef|grep nginx
5,查看版本
cd /usr/local/nginx/sbin
执行指令: ./nginx -v
执行指令: ./nginx -version
3,配置反向代理,配置文件目录/usr/local/nginx/conf/nginx.conf
location / {
root html;
# 反向代理配置proxy_pass
proxy_pass http://127.0.0.1:8080;
index index.html index.htm;
}
server {
listen 8000;
server_name 192.168.0.121;
location ~ /edu/ {
proxy_pass http://127.0.0.1:8001;
}
location ~ /vod/ {
proxy_pass http://127.0.0.1:8002;
}
}
# ~ 表示uri包含正则表达式,并且区分大小写
# ~* 表示uri包含正则表达式,并且不区分大小写
# = 表示uri不包含正则表达式,请求字符串必须严格匹配
# ^~ 表示uri不包含正则表达式,请求字符串前面必须匹配
4,配置负载均衡
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;
upstream mybackend {
server 192.168.0.121:8001;
server 192.168.0.121:8002;
}
#gzip on;
server {
listen 8000;
server_name 192.168.0.121;
location / {
proxy_pass http://mybackend;
root html;
index index.html index.htm;
}