工具指南 - nginx
一、常用配置
1.1 配置web
nginx
可以作为web
服务器,用于提供静态文件和动态页面的访问服务。由于nginx
具有高并发和高吞吐量的特点,所以在高访问量的web
应用中表现出色。
nginx
可以作为反向代理服务器,将请求转发给后端服务器,实现负载均衡、高可用、安全等功能。反向代理服务器可以解决访问压力大、单点故障等问题,保证系统的稳定性和可靠性。
比如我们这里有一个前后端分离的博客管理项目,这里我们将静态资源放到nginx
上,由nginx
管理,动态请求转发给后端。
server {
listen 3001;
client_max_body_size 5m;
server_name localhost;
error_page 404 /index.html;
location / {
root /opt/blog/webapp; # 静态资源,前端项目目录
index index.html;
}
location /blog-api {
proxy_pass http://127.0.0.1:30999/; # 动态请求,设置后端代理服务器的地址(反向代理)
}
}
其中:
-
/opt/blog/webapp
:为前端项目存放路径; -
http://127.0.0.1:30999/
:为后端服务地址,所有http://ip/blog-api/xxx
的请求都会转发到http://127.0.0.1:30999/xxx
。
如果服务开启了双向认证:
server {
listen 3001 ssl;
client_max_body_size 5m;
server_name localhost;
ssl_certificate /opt/nginx_ssl/server.crt; #server公钥证书
ssl_certificate_key /opt/nginx_ssl/server.key; #server私钥
ssl_client_certificate /opt/nginx_ssl/client.crt;
ssl_verify_client on;
error_page 404 /index.html;
location / {
root /opt/blog/webapp; # 前端项目地址
index index.html;
}
location /blog-api {
proxy_pass https://127.0.0.1:30999/; # 设置后端代理服务器的地址(反向代理)
proxy_ssl_certificate /opt/nginx_ssl/client.crt; # client公钥证书
proxy_ssl_certificate_key /opt/nginx_ssl/client.key; # client私钥证书
}
}
1.2 获取客户端IP
在nginx
配置文件中,我们可以通过$remote_addr
变量来获取客户端IP
地址。这个变量不是nginx
私有的变量,通常我们使用它来记录客户端IP
地址。
server {
listen 3001 ;
client_max_body_size 5m;
server_name localhost;
error_page 404 /index.html;
location / {
root /opt/blog/webapp; # 前端项目地址
index index.html;
}
location /blog-api {
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:30999/; # 设置后端代理服务器的地址(反向代理)
}
}
1.3 通过代理服务器获取客户端IP
在某些情况下,我们的nginx
前面可能会有一个或多个代理服务器,而客户端的真实IP地址可能会被代理服务器隐藏掉,这时我们就需要通过代理服务器来获取客户端真实IP地址。
在代理服务器中,我们需要通过add_header
指令来设置X-Real-IP
头部信息。然后在nginx
中,我们需要使用$proxy_add_x_forwarded_for
变量来获取客户端IP
地址。这个变量的值包括代理服务器的IP
地址和客户端的IP
地址。
代理服务器配置:
location / {
proxy_pass http://backend;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
nginx
服务器配置:
location / {
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log;
#获取客户端IP地址,使用$proxy_add_x_forwarded_for变量
add_header X-Real-IP $proxy_add_x_forwarded_for;
}
1.4 负载均衡
亲爱的读者和支持者们,自动博客加入了打赏功能,陆陆续续收到了各位老铁的打赏。在此,我想由衷地感谢每一位对我们博客的支持和打赏。你们的慷慨与支持,是我们前行的动力与源泉。
日期 | 姓名 | 金额 |
---|---|---|
2023-09-06 | *源 | 19 |
2023-09-11 | *朝科 | 88 |
2023-09-21 | *号 | 5 |
2023-09-16 | *真 | 60 |
2023-10-26 | *通 | 9.9 |
2023-11-04 | *慎 | 0.66 |
2023-11-24 | *恩 | 0.01 |
2023-12-30 | I*B | 1 |
2024-01-28 | *兴 | 20 |
2024-02-01 | QYing | 20 |
2024-02-11 | *督 | 6 |
2024-02-18 | 一*x | 1 |
2024-02-20 | c*l | 18.88 |
2024-01-01 | *I | 5 |
2024-04-08 | *程 | 150 |
2024-04-18 | *超 | 20 |
2024-04-26 | .*V | 30 |
2024-05-08 | D*W | 5 |
2024-05-29 | *辉 | 20 |
2024-05-30 | *雄 | 10 |
2024-06-08 | *: | 10 |
2024-06-23 | 小狮子 | 666 |
2024-06-28 | *s | 6.66 |
2024-06-29 | *炼 | 1 |
2024-06-30 | *! | 1 |
2024-07-08 | *方 | 20 |
2024-07-18 | A*1 | 6.66 |
2024-07-31 | *北 | 12 |
2024-08-13 | *基 | 1 |
2024-08-23 | n*s | 2 |
2024-09-02 | *源 | 50 |
2024-09-04 | *J | 2 |
2024-09-06 | *强 | 8.8 |
2024-09-09 | *波 | 1 |
2024-09-10 | *口 | 1 |
2024-09-10 | *波 | 1 |
2024-09-12 | *波 | 10 |
2024-09-18 | *明 | 1.68 |
2024-09-26 | B*h | 10 |
2024-09-30 | 岁 | 10 |
2024-10-02 | M*i | 1 |
2024-10-14 | *朋 | 10 |
2024-10-22 | *海 | 10 |
2024-10-23 | *南 | 10 |
2024-10-26 | *节 | 6.66 |
2024-10-27 | *o | 5 |
2024-10-28 | W*F | 6.66 |
2024-10-29 | R*n | 6.66 |
2024-11-02 | *球 | 6 |
2024-11-021 | *鑫 | 6.66 |
2024-11-25 | *沙 | 5 |
2024-11-29 | C*n | 2.88 |

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了