案例
2025年9月5日大约 4 分钟
拒绝其他域名访问
# 🚫 默认拒绝所有非匹配域名的 443 请求
server {
listen 443 ssl default_server;
server_name _;
# 这里必须要有证书,否则 TLS 握手会失败
# 可以放一个自签名证书,或者跟你主域名同一个证书
ssl_certificate /etc/letsencrypt/live/cs.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/cs.com/privkey.pem;
return 444; # 直接断开连接(比403更安全)
}
# ✅ 只对 cs.com 提供服务
反向代理域名
server {
listen 81;
server_name 对外暴露的域名;
location / {
proxy_pass http://源站ip;
proxy_set_header Host cs.com; # 源站需要登录的域名
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;
}
}
回源代理
明白 ✅
你说 192.168.3.4 上的服务必须用域名访问,不能直接用 IP。
这通常是因为:
- 后端服务做了 虚拟主机 (vhost) 绑定,只响应
catpd.cn
或其他域名; - SSL 证书只匹配域名,不匹配裸 IP。
解决思路
你需要在 192.168.3.2 / 192.168.3.3 上配置 反向代理,并让代理转发时保留域名(Host 头)而不是替换成 IP。
Nginx 配置示例
假设后端服务只认 catpd.cn
这个域名:
upstream backend {
server 192.168.3.4;
}
server {
listen 80;
server_name catpd.cn;
location / {
proxy_pass http://backend;
proxy_set_header Host catpd.cn; # 强制带上域名
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
如果是 HTTPS 回源:
upstream backend {
server 192.168.3.4:443;
}
server {
listen 443 ssl;
server_name catpd.cn;
ssl_certificate /etc/nginx/ssl/catpd.cn.crt;
ssl_certificate_key /etc/nginx/ssl/catpd.cn.key;
location / {
proxy_pass https://backend;
proxy_ssl_server_name on; # 启用 SNI
proxy_ssl_name catpd.cn; # 指定 SNI 域名
proxy_set_header Host catpd.cn; # 保留域名
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
这样:
- DNS 依然把
catpd.cn
→192.168.3.2 / 192.168.3.3
- 前端机器接收到请求后,会用 域名 catpd.cn 去请求 192.168.3.4
- 后端服务器认为自己就是在服务
catpd.cn
,正常返回
关键点
- 不能直接 proxy_pass 到 IP,要么写域名,要么在
proxy_set_header Host
强制指定域名; - 如果后端是 HTTPS,必须启用
proxy_ssl_server_name on;
并指定proxy_ssl_name
,否则证书校验会失败。
要不要我帮你直接写一个 CoreDNS 配置 + Nginx 反代完整组合方案(把 DNS 和反代一体化串起来)?
缓存
# ✅ 缓存图片资源:只对 /images/ 路径启用缓存
location ~* ^/images/.*\.(png|jpg|jpeg|gif|webp)$ {
proxy_pass http://ip;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache my_cache;
proxy_cache_valid 200 302 365d;
proxy_cache_valid 404 1m;
proxy_ignore_headers Cache-Control Expires;
proxy_cache_use_stale updating error timeout http_500 http_502 http_503 http_504;
add_header X-Cache-Status $upstream_cache_status;
}
# 🚫 不缓存 JS、CSS、HTML
location ~* \.(js|css|html)$ {
proxy_pass http://ip;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
# 显式禁用缓存
add_header X-Cache-Status "BYPASS";
}
# 其他资源(默认不缓存)
location / {
proxy_pass http://ip;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
# 不使用 proxy_cache
add_header X-Cache-Status "BYPASS";
}
代理vite
base: '/easybd',
server {
listen 80;
server_name localhost;
location /easybd {
alias /usr/share/nginx/html;
index index.html index.htm;
#若不配置try_files,刷新会404
try_files $uri $uri/ /easybd/index.html; # 注意这里需要加上base路径
}
}
代理vue
export default defineUserConfig({
base: "/ddstudy/",
lang: "zh-CN",
theme,
// Enable it with pwa
// shouldPrefetch: false,
});
server {
listen 5066;
server_name localhost;
location ^~ /ddstudy {
alias /home/soft/schooletl/dist;
index index.html index.htm;
}
location /api {
proxy_pass http://127.0.0.1:5002;
proxy_set_header Host $host;
}
}
代理数据库
pgsql
# 需要放在nginx.conf 最后
stream {
upstream cloudsocket {
hash $remote_addr consistent;
# $binary_remote_addr;
server db:5432 weight=5 max_fails=3 fail_timeout=30s;
}
server {
listen 9080;#数据库服务器监听端口
proxy_connect_timeout 10s;
proxy_timeout 300s;#设置客户端和代理服务之间的超时时间,如果5分钟内没操作将自动断开。
proxy_pass cloudsocket;
}
}
mysql
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
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"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 81;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
stream {
upstream cloudsocket {
hash $remote_addr consistent;
# $binary_remote_addr;
server 数据库IP:数据库地址 weight=5 max_fails=3 fail_timeout=30s;
}
server {
listen 80;#数据库服务器监听端口
proxy_connect_timeout 10s;
proxy_timeout 300s;#设置客户端和代理服务之间的超时时间,如果5分钟内没操作将自动断开。
proxy_pass cloudsocket;
}
}
代理目录
location / {
root /data/EMR;
autoindex on;
}
代理资源
server {
listen 88;
location / {
auth_basic "need user and password";
auth_basic_user_file /etc/nginx/conf.d/pass_file;
root /packages;
autoindex on;
}
}
location / {
root /home/shingi/soft/web/dist;
index index.html index.htm;
try_files $uri /index.html;
}
location ^~ /api/ {
set $proxy_method $request_method;
if ( $http_x_custommethod != '') {
set $proxy_method $http_x_custommethod;
}
proxy_method $proxy_method;
proxy_pass http://localhost:10100;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
location ^~ /api/fastapi/ {
proxy_pass http://localhost:3302/api/fastapi/;
}
代理jupyter notebook
生成配置文件
jupyter notebook --generate-config
设置密码
jupyter notebook password
更改 .jupyter/jupyter_notebook_config.py
c.ServerApp.base_url = '/nb/'
代理
location /nb {
proxy_pass http://172.25.101.28:10088/nb;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-Scheme $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# WebSocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 120s;
proxy_next_upstream error;
proxy_redirect off;
proxy_buffering off;
}
basic认证
1. 生成一个账户密码文件 pass_file
htpasswd -c -d /etc/nginx/conf.d/pass_file fd
fd ---用户名
2. nginx.conf
auth_basic "need user and password";
auth_basic_user_file /etc/nginx/conf.d/pass_file;
实例
location / {
auth_basic "need user and password";
auth_basic_user_file /etc/nginx/conf.d/pass_file;
root /usr/share/nginx/html;
index index.html index.htm;
}