正文
一、提前条件
负载均衡:nginx.tar.gz
云服务器:CentOS 8
二、安装过程
1.安装必要插件
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
参数解释:
1.gcc用于编译c(nginx是由c开发的)
2.pcre pcre-devel是一个库,包括一些正则表达式库(pcre来解析正则表达式,因此nginx支持正则表达式匹配)
3.zlib zlib-devel提供多种压缩和解压的方式(nginx使用zlib对http包进行gzip)
4.openssl openssl-devel是用来数据传输加密,防止数据泄露(nginx因此能够去安装ssl证书)
2.解压
tar -zxvf nginx.tar.gz
3.开始安装-指定安装路径
./configure --prefix=/laokou/nginx
4.编译
make && make install
5.启动
cd /laokou/nginx/sbin
./nginx
6.配置(反向代理网关)
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; add_header 'Access-Control-Allow-Origin' "$http_origin" always; add_header 'Access-Control-Allow-Credentials' 'true' always; add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS' always; add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified- ?Since,Keep-Alive,Origin,User-Agent,X-Requested-With' always; location / { proxy_pass http://127.0.0.1:1234; proxy_connect_timeout 6000; proxy_read_timeout 6000; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_set_header Connection ""; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
7.重启
./nginx -s reload