Linux CentOS7上安装Nginx,以及反向代理配置,转发服务请求,根据请求链接转发到对应服务,负载均衡配置。

    科技2025-07-23  8

    1、Nginx依赖组件的安装

    1.1、openssl安装

    openssl下载 进入官网点击Downloads https://www.openssl.org/source/ 下载安装包 文件上传服务器后按顺序执行如下命令安装

    tar -zxvf openssl-fips-2.0.16.tar.gz

    cd openssl-fips-2.0.16/

    ./config

    make

    make install

    查看是否安装成功,执行如下命令输出下图内容说明安装成功

    rpm -qa|grep -i openssl

    1.2、zlib安装

    zlib下载: 进入官网http://www.zlib.net/ 点击红框位置下载 文件上传服务器后按顺序执行如下命令安装

    tar -zxvf zlib-1.2.11.tar.gz

    cd zlib-1.2.11/

    ./configure

    make

    make install

    查看是否安装成功,执行如下命令输出下图内容说明安装成功

    rpm -qa|grep -i zlib

    1.3、pcre安装

    pcre下载https://ftp.pcre.org/pub/pcre/ 文件上传服务器后按顺序执行如下命令安装

    tar -zxvf pcre-8.44.tar.gz

    cd pcre-8.44/

    ./configure

    make

    make install

    查看是否安装成功,执行如下命令输出下图内容说明安装成功

    rpm -qa|grep -i pcre

    或者使用

    pcre-config --version

    2、Nginx安装

    安装包下载 进入官网点击下载对应版本

    http://nginx.org/en/download.html

    文件上传服务器后按顺序执行如下命令安装

    tar -zxvf nginx-1.18.0.tar.gz

    cd nginx-1.18.0/

    ./configure --with-pcre=…/pcre-8.44 --with-zlib=…/zlib-1.2.11/ --with-openssl=…/openssl-fips-2.0.16

    make

    make install

    查看是否安装成功,执行如下命令输出下图内容说明安装成功

    cd /usr/local/nginx/sbin

    ./nginx -t

    启动Nginx

    cd /usr/local/nginx/sbin

    ./nginx

    查看进程

    ps -ef | grep nginx

    查看端口号,可以看到是80

    netstat -ntlp

    通过IP访问端口80出现以下页面说明正常启动

    3、Nginx配置

    配置文件 进入Nginx安装目录nginx-1.18.0

    cd conf

    vi nginx.conf

    #user nobody;# Nginx Worker进程运行用户 worker_processes 1;# 指定Nginx开启的进程数 #error_log logs/error.log;# 全局错误日志文件,日志输出级别有debug、info、notice、warn、error、crit可供选择 #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid;# 指定进程pid的存储文件位置 events { worker_connections 1024;# 设定Nginx的工作模式及连接数上限 } http { include mime.types;# include是个主模块指令,实现对配置文件所包含的文件的设定,可以减少主配置文件的复杂度。 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"';# 日志输出格式,main为此日志输出格式的名称。 #access_log logs/access.log main;# 指定Nginx的访问日志存放路径 sendfile on;# 开启高效文件传输模式。可以将tcp_nopush和tcp_nodelay两个指令设置为on用于防止网络阻塞; #tcp_nopush on; #keepalive_timeout 0;# 设置客户端连接保持活动的超时时间。在超过这个时间之后,服务器会关闭该连接; keepalive_timeout 65; #gzip on;# 设置开启或者关闭gzip模块,“gzip on”表示开启GZIP压缩,实时压缩输出数据流; server { listen 80;# 指定虚拟主机的服务端口 server_name localhost;# 监听服务IP或者域名,多个域名之间用空格分开 #charset koi8-r;# 设置网页的默认编码格式。 #access_log logs/host.access.log main;# 指定此虚拟主机的访问日志存放路径,最后的main用于指定访问日志的输出格式。 location / {# 当访问项目根路径时,首先访问html/index.html,如果没有则访问html/index.htm root html;# 根目录 index index.html index.htm;# 根目录下的文件 } #error_page 404 /404.html;# 404页面 # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html;# 50#错误页面 location = /50x.html {# 访问/50x.html文件时,在根目录html下查找 root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }

    4、Nginx配置反向代理

    4.1、简单服务请求转发

    我们目标:访问服务器的80端口,由Nginx转发到我们真实的服务器部署端口8888

    修改配置文件 cd /usr/local/nginx/conf/

    vi nginx.conf 如下位置添加proxy_pass http://127.0.0.1:8888;

    location / { root html; proxy_pass http://127.0.0.1:8888; index index.html index.htm; }

    保存后执行如下命令重新加载配置文件

    cd /usr/local/nginx/sbin

    ./nginx -s reload

    访问我们的服务,确保服务正常运行 输入IP访问Nginx确认正常运行 访问我们的服务,把端口换成80或者不输入端口号 如下正常返回说明Nginx正常转发了请求

    4.2、根据请求连接不同转发不同服务

    在服务器上准备一个8888端口的服务,和一个9999端口的服务

    修改Nginx配置文件内容如下,并重新加载配置文件

    location ~ /mary/ { proxy_pass http://127.0.0.1:8888; } location ~ /tom/ { proxy_pass http://127.0.0.1:9999; }

    访问我们两个服务的路径,正常返回说明配置生效

    4.3、负载均衡

    首先在两个8888和9999端口的服务都添加一个/mary/hello的接口,把返回的端口号改成对应的端口

    @RestController @RequestMapping("/mary") public class JenkinsController { @GetMapping("/hello") public String hello(){ return "Good ! You have started springboot by jenkins ! The port is ****!"; } }

    按照下图修改配置文件,并重新加载

    upstream myserver{ server localhost:8888; server localhost:9999; } location ~ /mary/ { proxy_pass http://myserver; }

    访问/mary/hello端口,浏览器中不断刷新页面,在下面两个结果切换,说明配置负载均衡生效

    Processed: 0.033, SQL: 8