NginxProxyManager(下文简称NPM)是一个很好用的反代管理器,部署web项目网上很多教程,但是Nginx也可以作为静态资源服务器(html,css,image)等,以下是托管HTML页面的教程.
具体方法就是就是在NPM的每个反代server块中设置location块指定不同路径的请求.
由于作者的NginxProxyManager是运行在Docker上的,因此静态资源存放于挂载的.../data/static
文件夹内,请根据自身需求更改.以下是作者的静态资源文件夹结构:
📂 data └── 📂 static ├── 📄 colorclock.html └── 📂 ip └── 📄 ip.html
在NPM页面新配置一个代理Add Proxy Host
:
然后在Advanced
选项卡中修改Custom Nginx Configuration
并添加location
块:
location / { root /data/static; index colorclock.html; }
提示
上文index指令用于指定请求URL为/时,Nginx应返回哪些文件作为首页.
(本文以colorclock.html
示例,请自行修改)
最后点击右下角Save
即可.这样,就可以通过访问域名/
来访问配置的静态资源.
至此,大功告成.
说是进阶配置,其实就是如何运用location
块的问题,熟悉Nginx的同学可以退出了.
ip
文件夹,可以修改location
块如下:location /ip/ { root /data/static/; index ip.html; }
这样,当请求URL为域名/ip/
时,Nginx会匹配ip
目录内设定index名为ip.html
的文件.
expires
命令来控制页面缓存,expires
命令通过给资源设定过期时间,由浏览器自行确认是否过期,无需再次请求服务器确认.location / { expires 7d; }
注
设置expires
后,如果是通过浏览器缓存访问资源,返回状态码304;
如果是从服务器重新下载,返回状态码200
如果更改了资源发现本地访问没有变化,可以通过此方法确认.
error_page
来修改请求路径location / { error_page 404 /index.html; }
proxy_pass
命令重新发起请求location /blog { proxy_pass http://eslyf.cn/; }
当访问域名/blog时将重新请求(可以是主机名称
,IP:端口
,UNIX-domain套接字路径
等形式)
location / { default_type text/html; add_header Content-Type "text/html; charset=utf-8"; return 200 '<div>该网站域名已更改,请访问新域名:<a href="https://eslyf.cn" target="_blank">博客</a></div>'; }
location /ip/ { root /data/static/; index ip.html; }
location /ip { root /data/static/; index ip.html; }
location /blog { proxy_pass http://eslyf.cn/; }
此时,proxy_pass
上的URL有资源路径/
,那么location
的/blog
会被替换成/
,Nginx代理后的请求变为:http://eslyf.cn/
location /blog { proxy_pass http://eslyf.cn; }
此时,proxy_pass
上的URL没有资源路径,那么location
的/blog
会匹配到Nginx代理后的URL中,Nginx代理后的请求变为:http://eslyf.cn/blog
location /blog/ { proxy_pass http://eslyf.cn; }
此时,proxy_pass
上的URL没有资源路径,Nginx会做特殊处理,访问http://eslyf.cn/blog
等同于http://eslyf.cn/blog/
,如果你想这两种请求对应不同的处理,就要明确增加不带/
结尾的location
配置
location /blog { proxy_pass http://eslyf.cn; } location /blog/ { proxy_pass http://test.eslyf.cn; }
客户端请求URL | location上的URL | proxy_pass上的URL | Nginx代理后发送给服务器的请求URL |
---|---|---|---|
test.eslyf.cn | /blog/ | test.eslyf.cn | test.eslyf.cn/blog |
test.eslyf.cn | /blog/ | test.eslyf.cn/vlog | test.eslyf.cn/vlog |
test.eslyf.cn | /blog | test.eslyf.cn | test.eslyf.cn/blog |
test.eslyf.cn | /blog | test.eslyf.cn/vlog | test.eslyf.cn/vlog |
本文作者:EsLyF
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!