背景:现实生产环境中,部署的环境经常是离线的,通常要经过跳板机才能到生产机器上,且生产机器不带web界面,导致需要进行测试web的时候无法测试,这时候就很需要一个nginx代理
机器环境:a为windows或带web的linux,充当客户端,b为跳板机,c为生产机器
在线/离线安装nginx
在线安装
# 安装所需环境
[root@nginx data]# yum install -y openssl-devel pcre-devel
# 解压
[root@nginx data]# tar zxf nginx-1.24.0.tar.gz
# 创建工作目录
[root@nginx data]# mkdir -p /apps/nginx
# 安装
[root@nginx data]# cd nginx-1.24.0/
[root@nginx nginx-1.24.0]# ./configure --prefix=/apps/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-file-aio --with-http_gzip_static_module
[root@nginx nginx-1.24.0]# make && make install
# 查看结果
[root@nginx nginx-1.24.0]# /apps/nginx/sbin/nginx -V
nginx version: nginx/1.24.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-file-aio --with-http_gzip_static_module
[root@nginx nginx-1.24.0]# cd /apps/nginx/
# 启动;并查看启动结果
[root@nginx nginx]# ./sbin/nginx
# 访问测试
[root@nginx nginx]# curl 127.0.0.1:80
离线安装
1.准备环境
# 准备离线安装包
nginx:http://nginx.org/en/download.html (版本:nginx-1.24.0 tar.gz)
pcre:https://sourceforge.net/projects/pcre/files/pcre/(版本:pcre-8.40.tar.gz)
zlib:http://www.zlib.net/(版本:zlib-1.3.1.tar.gz)
oppenssl:https://www.openssl.org/source/(版本:openssl-3.2.1.tar.gz)
-----------------------------------------------------------------------------------
[root@nginx data]# ls
nginx-1.24.0.tar.gz openssl-3.2.1.tar.gz pcre-8.40.tar.gz zlib-1.3.1.tar.gz# 准备工作目录
[root@nginx ~]# mkdir -p /data/unit/{pcre,zlib,openssl,nginx} && cd /data
2.依次安装依赖
安装PCRE
[root@nginx data]# tar -zxvf pcre-8.40.tar.gz
[root@nginx data]# cd pcre-8.40/
[root@nginx pcre-8.40]# ./configure --prefix=/data/unit/pcre
[root@nginx pcre-8.40]# make && make install
安装zlib
[root@nginx data]# tar -zxvf zlib-1.3.1.tar.gz
[root@nginx data]# cd zlib-1.3.1/
[root@nginx zlib-1.3.1]# ./configure --prefix=/data/unit/zlib
[root@nginx zlib-1.3.1]# make && make install
安装openssl
[root@nginx data]# tar -zxvf openssl-3.2.1.tar.gz
[root@nginx data]# cd openssl-3.2.1/
[root@nginx openssl-3.2.1]# ./config --prefix=/data/unit/openssl --shared
[root@nginx openssl-3.2.1]# make && make install
# 出现安装失败问题
[root@nginx openssl-3.2.1]# ./config --prefix=/data/unit/openssl --shared
Can't locate IPC/Cmd.pm in @INC (@INC contains: /data/openssl-3.2.1/util/perl /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 . /data/openssl-3.2.1/external/perl/Text-Template-1.56/lib) at /data/openssl-3.2.1/util/perl/OpenSSL/config.pm line 19.
BEGIN failed--compilation aborted at /data/openssl-3.2.1/util/perl/OpenSSL/config.pm line 19.
Compilation failed in require at /data/openssl-3.2.1/Configure line 23.
BEGIN failed--compilation aborted at /data/openssl-3.2.1/Configure line 23.
# 解决方法
[root@nginx openssl-3.2.1]# yum install perl-IPC-Cmd
安装nginx
[root@nginx data]# tar -zxvf nginx-1.24.0.tar.gz
[root@nginx data]# cd nginx-1.24.0/
[root@nginx nginx-1.24.0]# ./configure --prefix=/data/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-pcre=/data/pcre-8.40 \
--with-zlib=/data/zlib-1.3.1 \
--with-openssl=/data/openssl-3.2.1 \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-pcre \
--with-file-aio \
--with-http_gzip_static_module
[root@nginx ~]# cd /data/nginx/sbin/
[root@nginx sbin]# ./nginx
[root@nginx data]# make && make install
# 查看结果
[root@nginx sbin]# curl 127.0.0.1:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
启动nginx
[root@nginx ~]# cd /data/nginx/sbin/
./nginx # 启动服务
./nginx -s reload # 重新载入配置文件
./nginx -s reopen # 重启 Nginx
./nginx -s stop # 停止 Nginx
-----------------------------------------------------------------------------------
# 添加启动文件即可通过systemctl命令来启动nginx
[root@bogon conf]# vi /lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/data/nginx/logs/nginx.pid
ExecStartPre=/data/nginx/sbin/nginx -t
ExecStart=/data/nginx/sbin/nginx
ExecReload=/data/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
# 重载systemctl服务
[root@nginx sbin]# systemctl daemon-reload
-----------------------------------------------------------------------------------
# 检查配置文件
[root@nginx nginx]# /data/nginx/sbin/nginx -t
nginx: the configuration file /data/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /data/nginx/conf/nginx.conf test is successful
配置反向代理
以上配置建议基于B与C已经互通的假设,并且假设您希望在B上配置Nginx作为反向代理。根据您的具体需求和环境,配置可能会有所不同。
-
编辑Nginx的配置文件(通常位于/etc/nginx/nginx.conf或/etc/nginx/sites-available/default)。
-
添加一个server块,设置监听端口(如80或443,如果您打算使用HTTPS)和代理目标(即C机器的IP地址和Web服务端口)。
-
server { listen 80; server_name your_domain_or_ip; #代理机器 访问该机器80端口时被转发到proxy_pass location / { proxy_pass http://C_machine_ip:80; #被代理的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; proxy_set_header X-Forwarded-Proto $scheme; } }
-
重启Nginx服务:
- 使用systemctl或service命令重启Nginx服务,使新的配置生效。
-
访问Web服务
-
在Windows机器上,通过浏览器访问B机器上Nginx监听的端口(即您在Nginx配置文件中设置的端口,如80)。
-
浏览器将请求发送到B机器上的Nginx,Nginx将请求转发到C机器上的Web服务。
-
C机器上的Web服务处理请求并返回响应,响应通过Nginx返回给浏览器。
-
注意事项
-
确保B机器和C机器之间的网络连接是通畅的。
-
确保Nginx的配置是正确的,并且Nginx服务已经启动。
-
如果您打算使用HTTPS,您需要在Nginx上配置SSL证书和密钥。
-
考虑到安全性和隐私性,确保SSH隧道和Nginx代理都配置了适当的身份验证和访问控制。