我在生產服務器上有一個在 Node Express localhost:4000 上運行的 Angular 應用程式(使用 Universal 進行服務器端渲染),并且我為該應用程式配置了 Nginx 反向代理。生產服務器使用 HTTPS 作為其域名。
這是 /etc/config/sites-enabled 中的 nginx 配置:
location ~ (/landing/home|/landing/company|/landing/contact|/landing/support) {
proxy_pass http://127.0.0.1:4000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache_bypass $http_upgrade;
add_header X-Cache-Status $upstream_cache_status;
}
location / {
proxy_pass http://127.0.0.1:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
這是 nginx.conf
user ubuntu;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 2048;
multi_accept on;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
server_names_hash_bucket_size 64;
server_name_in_redirect off;
# include /etc/nginx/mime.types;
# default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
在 Chrome 開發工具 - 網路中,這里是影像(SVG 檔案)的示例請求和回應。nginx 發送的影像是舊版本,并且已經在 Angular 端進行了更新(檔案名未更改)。請注意,這個檔案只是一個示例,我面臨的問題不僅僅是這個檔案,而是所有靜態檔案(包括 css 和 js 檔案)。
請求頭
回應頭
為了驗證,我確實在客戶端和服務器上卷曲到同一個影像檔案。這是卷曲的結果:
curl 來自客戶端瀏覽器的結果,結果來自 nginx
服務器上的 curl 結果,比較 curl 到 localhost:4000 和 curl 到實際的公共 url
我們可以看到,來自 localhost:4000 的回應是影像的最新版本,而來自公共 url 的回應是同一影像的舊版本。
我檢查了 /etc/nginx,里面沒有 chache 檔案夾。我想過清除 nginx 的快取,但在那里找不到。
我嘗試在配置中添加很多東西,包括:
add_header Last-Modified $date_gmt;
add_header Cache-Control 'no-store, no-cache';
if_modified_since off;
expires off;
etag off;
和
proxy_cache off;
不知何故,即使 X-Cache-Status 也沒有出現在回應標頭中,但是比較來自 localhost 和公共 url 的 curl 結果,我很清楚它一定與 nginx 有關。
任何人都對如何使 nginx 從 localhost:4000 的實際輸出而不是從快取發送回應有什么建議?謝謝你。
更新#1:
抱歉,我只包含了部分 nginx 配置。我找到了根本原因:我實際上在同一個域上運行了兩個 Node Express,一個在埠 4000(Angular 通用)上,另一個在埠 5000(非通用)上。我已經更新了上面 nginx conf 的摘錄,以包括埠 5000 上的另一個位置指令。請參閱下面的答案,以進一步解釋我做錯了什么導致這個問題的問題。
uj5u.com熱心網友回復:
我找到了問題的根本原因。
我實際上有兩個 Node Express 在同一臺服務器上運行,同一個域。一個在埠 4000(使用 Angular Universal),另一個在埠 5000(非通用)。我已經編輯了我的問題以在 nginx conf 中包含第二個位置指令。
我的 nginx conf 使整個頁面看起來像是來自 localhost:4000 的回應,但頁面中的某些部分(影像、樣式表等)實際上是來自 localhost:5000 的回應,因為請求與 localhost:4000 的 nginx conf 中的模式不匹配。所以 localhost:5000 必須回應請求,并且 localhost:5000 擁有的檔案是舊版本(不是全部,但我用 curl 測驗的檔案恰好是舊版本)。
當我在 nginx conf 中禁用第二個 location 指令時,我才意識到這種情況,有效地禁用了 localhost:5000 回應任何請求,然后我因此看到了許多 404 錯誤。
為了解決這個問題,意味著讓 localhost:4000 和 localhost:5000 都處于活動狀態,并且仍然得到正確的回應,我不得不對我的 Angular 代碼中的路由進行一些調整。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/511184.html
標籤:nginx角通用
