Blage's Coding Blage's Coding
Home
算法
  • 手写Spring
  • SSM
  • SpringBoot
  • JavaWeb
  • JAVA基础
  • 容器
  • Netty

    • IO模型
    • Netty初级
    • Netty原理
  • JVM
  • JUC
  • Redis基础
  • 源码分析
  • 实战应用
  • 单机缓存
  • MySQL

    • 基础部分
    • 实战与处理方案
    • 面试
  • ORM框架

    • Mybatis
    • Mybatis_Plus
  • SpringCloudAlibaba
  • MQ消息队列
  • Nginx
  • Elasticsearch
  • Gateway
  • Xxl-job
  • Feign
  • Eureka
  • 面试
  • 工具
  • 项目
  • 关于
🌏本站
🧸GitHub (opens new window)
Home
算法
  • 手写Spring
  • SSM
  • SpringBoot
  • JavaWeb
  • JAVA基础
  • 容器
  • Netty

    • IO模型
    • Netty初级
    • Netty原理
  • JVM
  • JUC
  • Redis基础
  • 源码分析
  • 实战应用
  • 单机缓存
  • MySQL

    • 基础部分
    • 实战与处理方案
    • 面试
  • ORM框架

    • Mybatis
    • Mybatis_Plus
  • SpringCloudAlibaba
  • MQ消息队列
  • Nginx
  • Elasticsearch
  • Gateway
  • Xxl-job
  • Feign
  • Eureka
  • 面试
  • 工具
  • 项目
  • 关于
🌏本站
🧸GitHub (opens new window)
  • SpringCloudAlibaba

  • MQ消息队列

  • Nginx

    • Nginx
      • 1.Nginx配置文件
      • 2.下载安装
        • 2.1编译工具+原始安装包
        • 2.2使用yum工具
      • 3.Nginx命令
      • 4.Nginx配置文件块
      • 5.前端项目部署,配置代理目标服务器ip
      • 6.Docker部署
    • Nginx配置问题
  • Elasticsearch

  • Gateway

  • Xxl-job

  • Feign

  • Eureka

  • 中间件
  • Nginx
phan
2023-05-15
目录

Nginx

# Nginx

# 1.Nginx配置文件

一般来说Nginx都只是作为一个入口,连接外网到部署的微服务内网。

  • 首先客户端从外部直接访问一个域名/ip时,nginx会监听server块内server_name+listen,如果域名+端口匹配则会根据后续不同的url字段交给当前server块的不同location进行代理。
  • Nginx常常用来配置一个首页访问请求,整个流程:外部域名—>Nginx内server块代理到GateWay网关—>网关从根据Nacos转发给对应的微服务。
  • 而进入首页后的页面跳转,对于一个前端页面而言通常是前端页面向Gateway网关发送请求。
  • location块:
    • root:表示匹配的请求路径都去Nginx哪个文件夹下面找。
    • proxy_pass:表示所有请求都转发给哪个URL地址。

image-20230321202521773

# 2.下载安装

# 2.1编译工具+原始安装包

  • yum -y install gcc pcre-devel zlib-devel openssl openssl-devel安装C编译器依赖包
  • wget下载安装包
  • 解压tar -zxvf nginx-1.16.1.tar.gz
  • cd nginx-1.16.1
  • ./configure --prefix=/usr/local/nginx
  • make && make install

# 2.2使用yum工具

直接使用命令:

yum install -y nginx
1

一键安装完成,使用下面命令启动和关闭:

systemctl start nginx //启动
systemctl stop nginx //关闭
systemctl restart nginx //刷新配置
1
2
3

因为是源工具自动安装,注意两点:

  • 放前端打包文件的地址:/usr/share/nginx/html
  • 配置文件地址:/etc/nginx/nginx.conf

# 3.Nginx命令

执行命令要进入sbin目录

./nginx -v查看版本号
./nginx -t查看配置文件是否有问题

./nginx 启动nginx
./nginx -s stop 停止服务
./nginx -s reload 重新启动nginx
启动前关闭防火墙
systemctl stop firewalld
1
2
3
4
5
6
7
8

./表示linux的当前所在相对目录

/usr/local 表示绝对路径

# 4.Nginx配置文件块

①全局块②events③http(http全局块,server块)

部署静态资源:配置http的server块

反向代理:反向代理服务器为于用户与目标服务器之间,客户端不知道目标服务器的地址。

server{
	listen 82;
	server_name localhost;
	location/{
	 proxy_pass http://192...    填入代理的目标url地址
	}
}
1
2
3
4
5
6
7

负载均衡:代理服务服务器可以转发给一组服务器,根据负载均衡算法发送请求

upstream targetserver{  定义一组服务器
	server 192.168.138.101:8080;
	server 192.168.138.101:8081;
}
server{
	listen 82;
	server_name localhost;
	location/{
	 proxy_pass http://targetserver;    
	}
}
1
2
3
4
5
6
7
8
9
10
11

# 5.前端项目部署,配置代理目标服务器ip

注意这里除了要配置代理到前端服务器的index.html之外,还需要配置匹配拼接的后端请求路径的代理(location ^~ /prod-api/)。这是因为前端服务器代码内发送的请求,即使后端服务器在同一台机器+vite配置了代理,也会重新走外部nginx,因为此时发送请求的主机+域名依旧是对应服务器的IP。

server {
        listen       80;
        server_name  localhost;
location / {
        root   html/dist;
    index  index.html;
    }
    
location ^~ /api/ {
        rewrite ^/api/(.*)$ /$1 break;
        proxy_pass http://192.168.138.101:8080;  后端ip地址
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 6.Docker部署

见gateway网关项目

编辑 (opens new window)
#中间件#Nginx
上次更新: 2023/12/15, 15:49:57
消息队列面试
Nginx配置问题

← 消息队列面试 Nginx配置问题→

Theme by Vdoing | Copyright © 2023-2024 blageCoder
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式