前言
Nginx擅⻓处理静态⽂件,是⾮常好的图⽚、⽂件服务器。把所有的静态资源的放到nginx上,可以使应⽤动静分离,性能更 好。
一、配置图片静态代理
server {
listen 80;
server_name 域名地址;
server_name_in_redirect off;
#charset koi8-r;
#默认请求
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:81; #请求转向mysvr 定义的服务器列表
# proxy_connect_timeout 1;
}
set $allk_root_path "D:/pic";
location /imgPath{
alias $allk_root_path/imgPath;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
重点
set $allk_root_path "D:/pic";
location /imgPath{
alias $allk_root_path/imgPath;
}
二、配置验证码
说明:如在使用微信公众号开发时,配置安全域名和ip白名单时会要求验证码。
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
server_name 域名;
#rewrite ^(.*)$ https://${server_name}$1 permanent;
location /{
proxy_pass http://127.0.0.1:8888/;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
location /N0BFl8gO0B.txt{
default_type text/html;
return 200 "41f37afe8f6251ecb21ff70453a9a605";
}
location /okRNYuzL0C.txt{
default_type text/html;
return 200 "3435512708a5cffb65fe3bf8f7ba8291";
}
}
重点:
location /N0BFl8gO0B.txt{
default_type text/html; ##设置文本页面
return 200 "41f37afe8f6251ecb21ff70453a9a605"; ## 返回页面字符串
}
总结
nginx除了可以做服务器反向代理之外,还有很多用处。如配置图片静态代理,验证码等。