Nginx部署/反向代理配置

1.Nginx用途介绍

   主要技术:地址转换

   负载均衡:将客户单请求,按照一定的规则分配到一群服务器上,并将处理结果返回给相应的客户端
   作用:负责调度客户端请求、消除单点故障、减少错误返回结果、session持久化。
   反向代理:客户端请求,经过反向代理,分发到各服务器,然后服务器将返回结果交给反向代理,反向代理在返回给客户端
   作用:增强安全性、加速web访问速度、增强可伸缩性、灵活性
   二者区别:负载只有在服务器大于2台的时候才有意义,侧重将负载均衡到各服务器。(部署位置:反向代理-->负载均衡-->集群)

2.Nginx安装及配置详细教程
   (1)安装依赖包

   yum install -y pcre pcre-devel openssl openssl-devel gcc gcc gcc-c++ ncurses-devel perl useradd www -M -s /sbin/nologin  --创建用户

   (2)编译安装Nginx
   wget http://nginx.org/download/nginx-1.8.0.tar.gz --下载Nginx软件包
   tar -zxvf nginx-1.8.0.tar.gz

   编译前准备:
   vim auto/cc/gcc --取消Debug编译模式,179行
   #CFLAGS="$CFLAGS -g" (加上注释)

   cd nginx-1.8.0
   ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
   make && make install --编译安装

   (3)nginx启动
   cd /usr/local/nginx
   /usr/local/nginx/sbin/nginx --启动Nginx
   /usr/local/nginx/sbin/nginx -s reload/stop --关闭、重启

3.nginx反向代理配置
   (1)配置转发单台(单个ip)
   /usr/local/nginx/conf/nginx.conf
location / {
root html;
proxy_pass http://192.168.5.20:8090; (添加此行)
index index.html index.htm;
}

   (2)配置转发多个ip(多台服务器)
upstream redhatnginx{
server 192.168.59.137:9003;
}
upstream winserveriis{
server 192.168.59.157:80; (被代理的服务器ip地址+端口)
}
server {
listen 8081;
server_name testnginx.com;
location / {
proxy_pass http://redhatnginx;
index index.html index.htm;
}
}
server {
listen 8082; (代理端口)
server_name testwin.com; (域名)
location / {
proxy_pass http://winserveriis;
index index.html index.htm;
}
}
4.nginx反向代理(转发https/https卸载)

   内部http通过nginx转发为https,单台不用配置upstream
   先在Nginx上安装https证书:
   yum -y install mod_ssl
   openssl genrsa -out ca.key 2048
   openssl req -new -key ca.key -out ca.csr
   openssl x509 -req -days 36500 -in ca.csr -signkey ca.key -out ca.crt

   cp ca.crt /usr/local/nginx/conf/
   cp ca.key /usr/local/nginx/conf/
   cp ca.csr /usr/local/nginx/conf/

   然后配置:/usr/local/nginx/conf/nginx.conf (取消最后注释行,填写证书对应得路径,填写对应得ip)(upstream也需配置)
upstream winserveriis{
server 192.168.5.20:8090; (被代理的服务器ip地址+端口)
}
server {
listen 443 ssl;
server_name localhost;
ssl_certificate ca.crt;
ssl_certificate_key ca.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

location / {
proxy_pass http://192.168.5.20:8090/;
index index.html index.htm;
}
}

}

   配置完成,然后重启nginx:  /usr/local/nginx/sbin/nginx -s reload
   即可实现nginx反向代理和https卸载

posted on   枫飘过的天1  阅读(367)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
点击右上角即可分享
微信分享提示