转自我的个人博客《centos7 手动编译安装 Nginx 1.18.0》
一、安装相关依赖
yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
二、下载nginx 1.18.0
Nginx官网下载地址:http://nginx.org/download/nginx-1.18.0.tar.gz
cd /usr/local/src
wget http://nginx.org/download/nginx-1.18.0.tar.gz
三、解压
tar -zxvf nginx-1.18.0.tar.gz
四、进入nginx目录
cd nginx-1.18.0
五、配置 此处我们使用默认配置
./configure
六、编译、安装
make && make install
安装完成
我们查看一下安装好的nginx
cd /usr/local/nginx/
ll
total 0
drwxr-xr-x 2 root root 333 Nov 26 11:26 conf
drwxr-xr-x 2 root root 40 Nov 26 11:26 html
drwxr-xr-x 2 root root 6 Nov 26 11:26 logs
drwxr-xr-x 2 root root 19 Nov 26 11:26 sbin
七、配置
本篇仅写一下反代配置,也是使用nginx最多的一个功能
server {
listen 80;
server_name www.xxx.com;
location / {
proxy_pass http://localhost:8080;
client_max_body_size 100m;
#置主机头真实IP地址的用户避免获得为代理服务器的地址
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
过滤url中的路径配置
upstream prod {
server localhost:8081 weight=5;
}
upstream user {
server localhost:8082 weight=5;
}
server {
listen 80;
server_name test.com;
access_log "pipe:rollback /data/log/nginx/access.log interval=1d baknum=7 maxsize=1G" main;
location ^~/prod/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://prod/;
}
location ^~/user/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://user/;
}
}
说明:^~/prod/
表示匹配前缀是prod
的请求,proxy_pass
的结尾有/
, 则会把/prod/*
后面的路径直接拼接到后面,即移除user