Nginx代理转发配置

Nginx 的代理下载功能,真的是前端后端都爱用的老朋友了。配置简单,效率也高,适合资源下载这一类场景。

你只要在/etc/nginx.conf里加个server块,像下面这样:

server {
  listen 80;
  server_name localhost;
  location /download/ {
    proxy_pass http://backend_server:8080/download/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

用户一访问http://localhost/download/,求就被 Nginx 转发到你后端服务了,响应也快,资源也不容易掉链子。

想跑起来也不麻烦,Debian/Ubuntu上直接sudo apt install nginx就能装,CentOSyum。装完用systemctl控制服务启动、停止和开机自启,都挺方便。

还有个小技巧,如果你还想离线访问文档的功能,可以把文件扔到/var/www/html目录下。这样用户就能通过 Nginx 直接下载了,像个静态资源服务器一样用,挺灵活的。

你后端要是用的Node.js,Nginx 还可以加一层保护,帮你挡住一些奇奇怪怪的求。Node 应用监听8080,Nginx 外部求,性能上也能分担不少压力。

哦对了,配置完最好用nginx -t测试一下有没有写错,省得服务起不来。你要清掉服务,用sudo systemctl stop nginx就行。

如果你在搞资源分发、文档下载、或者接口聚合,Nginx 代理下载真的值得一用。配置熟练点之后,做些灰度发布、动静分离也不在话下。

zip
nginx-1.17.9.zip 预估大小:32个文件
folder
nginx-1.17.9 文件夹
file
清理Nginx.bat 28B
folder
temp 文件夹
folder
fastcgi_temp 文件夹
folder
uwsgi_temp 文件夹
folder
proxy_temp 文件夹
folder
client_body_temp 文件夹
folder
scgi_temp 文件夹
folder
docs 文件夹
file
CHANGES 296KB
file
README 49B
file
LICENSE 1KB
file
OpenSSL.LICENSE 6KB
file
PCRE.LICENCE 3KB
file
CHANGES.ru 451KB
file
zlib.LICENSE 983B
file
nginx.exe 3.54MB
folder
html 文件夹
file
50x.html 494B
file
index.html 612B
folder
logs 文件夹
file
nginx.pid 7B
file
access.log 720KB
file
error.log 98KB
folder
conf 文件夹
file
mime.types 5KB
file
uwsgi_params 681B
file
koi-utf 3KB
file
fastcgi_params 1KB
file
fastcgi.conf 1KB
file
win-utf 4KB
file
koi-win 2KB
file
scgi_params 653B
file
nginx.conf 3KB
folder
contrib 文件夹
file
README 543B
folder
vim 文件夹
folder
indent 文件夹
file
nginx.vim 250B
folder
ftplugin 文件夹
file
nginx.vim 29B
folder
ftdetect 文件夹
file
nginx.vim 198B
folder
syntax 文件夹
file
nginx.vim 127KB
file
geo2nginx.pl 1KB
folder
unicode2nginx 文件夹
file
koi-utf 6KB
file
win-utf 5KB
file
unicode-to-nginx.pl 1KB
zip 文件大小:1.66MB