Nginx反向代理靜態(tài)文件并修改路徑方式
更新時間:2025年03月19日 09:36:59 作者:草明
這篇文章主要介紹了Nginx反向代理靜態(tài)文件并修改路徑方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
Nginx反向代理靜態(tài)文件并修改路徑
Nginx 配置想要將 /a/b/ 的請求代理到本地目錄 /abc/ 下的文件??梢栽?Nginx 配置中使用 alias 指令來指定一個本地路徑作為代理目標。
server {
listen 8080;
location / {
proxy_pass http://192.168.1.100:8080;
}
location /a/b/ {
alias /abc/;
try_files $uri $uri/ /index.html;
}
}解釋和注意事項:
location /a/b/:這里配置了一個 location 塊,用于匹配以/a/b/開頭的請求。alias /abc/;:使用 alias 指令指定了本地路徑/abc/作為代理目標。當匹配到/a/b/的請求時,Nginx 將會將這些請求映射到本地目錄/abc/。try_files $uri $uri/ /index.html;:這里使用了try_files指令,用于嘗試查找對應的文件。如果請求的文件不存在,則會返回/index.html。
注意:
- 在使用
alias指令時,結尾的斜杠/是重要的,確保路徑設置正確。 - 需要確保 Nginx 對
/a/b/的訪問權限和路徑配置正確,以及本地目錄/abc/中包含所需的靜態(tài)文件或資源。 - 配置完成后,重啟或重新加載 Nginx,然后嘗試訪問
/a/b/下的資源,它應該會被代理到本地目錄/abc/中的對應文件。
Nginx反向代理+路徑重寫 配置
#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 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# 訪問方式:http://127.0.0.1/1/ng1/ --> http://127.0.0.1:82 這里和方式二主要區(qū)別在于rewrite和proxy_pass的位置。
location ~ /ng1/ {
proxy_pass http://127.0.0.1:81;
rewrite "^/(.*)/ng1\/(.*)$" /$2 break;
}
# 訪問方式:http://127.0.0.1/1/ng2/ --> http://127.0.0.1:82
location ~ /ng2/ {
rewrite "^/(.*)/ng2/(.*)$" /$2 break;
proxy_pass http://127.0.0.1:82;
}
# 訪問方式:http://127.0.0.1/ng3/ --> http://127.0.0.1:83
location /ng3/ {
rewrite ^/ng3/(.*)$ /$1 break;
proxy_pass http://127.0.0.1:83;
}
#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;
}
}
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Windows環(huán)境下Nginx?服務器?SSL?證書安裝部署操作過程
這篇文章主要介紹了Windows環(huán)境下Nginx?服務器?SSL?證書安裝部署,指導您如何在Windows Nginx 服務器中安裝 SSL 證書,本文給大家講解的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-10-10
nginx報錯:[emerg] getpwnam(“www“)failed問題及解決
這篇文章主要介紹了nginx報錯:[emerg] getpwnam(“www“)failed問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03
服務器報錯nginx?502?Bad?Gateway的原因及如何解決詳解
項目啟動時莫名其妙網站訪問不了,502 Bad Gateway,下面這篇文章主要給大家介紹了關于服務器報錯nginx?502?Bad?Gateway的原因及如何解決的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-06-06

