第一次使用 nginx。
我所擁有的:
我正在運行三個應用程式。
- 公共應用:http://localhost:8080;
- 管理應用程式:http://localhost:1000;
- 基礎設施應用:http://localhost:3000;
我正在嘗試做的事情:
我希望每個應用程式都使用其各自的資源(css、js 等)。
我已經在做什么:
在每個應用程式中,我已經在為它們各自的靜態檔案提供服務。
所以我在公共和管理應用程式中有這行代碼:
this.express.use('/css',express.static(path.resolve(__dirname, 'css')));
當直接訪問每個應用程式(使用其各自的門)例如(http://localhost:8080)時,它們各自的資源被找到并使用。
出了什么問題: 文通過Web服務器訪問和應用程式,但公共應用程式(門8080)除外,它們各自的資源都沒有被加載。
我試圖做的: 為基礎設施應用程式設定兩個額外的位置,但我不知道這是否是正確的做法。
nginx組態檔:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
proxy_http_version 1.1;
# PUBLIC
location / {
proxy_pass http://localhost:8080;
}
# ADMIN
location /admin {
proxy_pass http://localhost:1000;
}
# INFRA
location /api {
proxy_pass http://localhost:3000/api;
}
location /images {
proxy_pass http://localhost:3000/images;
}
location /fonts {
proxy_pass http://localhost:3000/fonts;
}
# ErrorPage
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
uj5u.com熱心網友回復:
修改管理應用程式以服務器上的靜態檔案/admin/css和/admin/js路徑。對 infra 應用程式執行相同操作,其靜態檔案將在路徑/infra/css上提供。/infra/js
管理應用程式:
this.express.use('/admin/css',express.static(path.resolve(__dirname, 'css')));
this.express.use('/admin/js',express.static(path.resolve(__dirname, 'js')));
基礎設施應用:
this.express.use('/infra/css',express.static(path.resolve(__dirname, 'css')));
this.express.use('/infra/js',express.static(path.resolve(__dirname, 'js')));
修改 nginx 檔案,使所有以 開頭的路徑/admin都將轉到您的管理應用程式,所有以 開頭的路徑/infra都將轉到 infra 應用程式。
# PUBLIC
location / {
proxy_pass http://localhost:8080/; #note the trailing slash
}
# ADMIN
location /admin/ { #note the trailing slash
proxy_pass http://localhost:1000/; #note the trailing slash
}
# INFRA
location /infra/ { #note the trailing slash
proxy_pass http://localhost:3000/; #note the trailing slash
}
假設您在默認埠上運行 nginx,即 80http://localhost/css現在將加載css公共應用程式,http://localhost/infra/css將加載css基礎應用程式http://localhost/admin/css并將加載相同的管理應用程式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/511185.html
標籤:节点.js表示nginx
