/var/www/dist/biographicalNotes/下面有一个Html文件 biographicalNotes.html,我实际的nginx代理是这样的
server { listen 8080; server_name localhost; root /var/www/dist; index index.html; location / { try_files $uri $uri/ /index.html; } # Allow access to static resources location ~* .(jpg|jpeg|png|gif|ico|css|js|svg)$ { expires max; add_header Cache-Control "public, max-age=31536000"; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /var/www/dist; } }
这段 Nginx 配置主要用于监听端口 8080,并且定义了一个基本的静态文件服务器。下面是对每个部分的解释:
但是我现在我要求 localhost:8080/description要去访问这个页面 /var/www/dist/biographicalNotes/biographicalNotes.html,现在又该如何去解决呢?
server { listen 8080; server_name localhost; root /var/www/dist; index index.html; location / { try_files $uri $uri/ /index.html; } location /description/ { alias /var/www/dist/biographicalNotes/; try_files $uri $uri/ /biographicalNotes.html; location ~* .(jpg|jpeg|png|gif|ico|css|js|svg)$ { expires max; add_header Cache-Control "public, max-age=31536000"; } } # Allow access to static resources location ~* .(jpg|jpeg|png|gif|ico|css|js|svg)$ { expires max; add_header Cache-Control "public, max-age=31536000"; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /var/www/dist; } }
于是我们里面加了一段,这段配置主要是用于处理访问路径以 /description/ 开头的请求。让我们逐步解释这个配置块:
location /description/ { alias /var/www/dist/biographicalNotes/; try_files $uri $uri/ /biographicalNotes.html; location ~* .(jpg|jpeg|png|gif|ico|css|js|svg)$ { expires max; add_header Cache-Control "public, max-age=31536000"; } }
location /description/ { ... }:
alias /var/www/dist/biographicalNotes/;:
try_files $uri $uri/ /biographicalNotes.html;:
location ~* .(jpg|jpeg|png|gif|ico|css|js|svg)$ { ... }:
总体而言,这段配置的目的是处理 /description/ 路径的请求,将其映射到指定的文件系统目录,并对其中的静态文件启用浏览器缓存。