我试图通过使用proxy_pass的变量来防止nginx在找不到上游时崩溃(如this stackoverflow thread中所述):
# Set front-dev in a variable to let nginx start even if it is not accessible (which is the case in production)
set $frontdev front-dev;
resolver 127.0.0.11 [::1]; # in a docker environment
location /dev/ {
proxy_pass http://$frontdev:4200/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
这样 nginx 启动时不会崩溃,但是当我连接到上游时,会返回一个空的 html 页面而不是资源。
Firefox 返回此错误:
Uncaught SyntaxError: expected expression, got '<' jquery.min.js:1
由于返回此文件而不是jquery.min.js
:
<!doctype html>
<html lang="en">
<head>
<title>Shanoir</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="assets/images/favicon.ico" type="image/x-icon">
<link href="assets/css/awesome.min.css" rel="stylesheet">
<script src="/auth/js/keycloak.js"></script>
<script defer type="text/javascript" src="assets/papaya-nojquery/jquery.min.js"></script>
<script defer type="text/javascript" src="assets/papaya-nojquery/papaya.js"></script>
<script defer type="text/javascript" src="assets/jszip.min.js"></script>
</head>
</head>
<body>
<app-root></app-root>
<script src="runtime.js" type="module"></script><script src="polyfills.js" type="module"></script><script src="styles.js" type="module"></script><script src="vendor.js" type="module"></script><script src="main.js" type="module"></script></body>
</html>
以下是一些 nginx 日志:
shanoir-ng-nginx | 172.18.0.1 - - [16/Nov/2020:15:55:09 +0000] "GET /dev/ HTTP/1.1" 200 408 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
shanoir-ng-nginx | 172.18.0.1 - - [16/Nov/2020:15:55:09 +0000] "GET /dev/assets/css/awesome.min.css HTTP/1.1" 200 408 "https://shanoir-ng-nginx/dev/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
shanoir-ng-nginx | 172.18.0.1 - - [16/Nov/2020:15:55:09 +0000] "GET /dev/polyfills.js HTTP/1.1" 200 408 "https://shanoir-ng-nginx/dev/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
[...]
笔记
此配置完美运行,但上游必须在 nginx 启动之前启动:
# Front development with angular-cli server
location /dev/ {
proxy_pass http://front-dev:4200/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
我不明白缺少什么,知道吗?