0

I tried to prevent nginx from crashing when the upstream is not found by using a variable for the proxy_pass (as explained in 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";
}

That way nginx starts without crashing, but an empty html page is returned instead of the resources when I connect to the upstream.

Firefox returns this error:

Uncaught SyntaxError: expected expression, got '<'    jquery.min.js:1

Since this file is returned instead of 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>

Here are some nginx logs:

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" "-"
[...]

Note

This config works perfectly, but the upstream must be up before nginx starts:


# 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";
}

I don't understand what is missing, any idea?

arthur.sw
  • 11,052
  • 9
  • 47
  • 104
  • 2
    408 is the length of the response in bytes. The status code is 200. – Richard Smith Nov 16 '20 at 16:23
  • ahah thanks ^^! I updated my question. I must check that the upstream is indeed started when nginx initializes ; maybe my upstream takes too much times to start up and gets available after nginx initializes. I'll confirm that in a minute. – arthur.sw Nov 17 '20 at 09:10
  • ok I get the same problem even when my upstream is available when nginx starts. – arthur.sw Nov 17 '20 at 09:19
  • 2
    You have a trailing `/` on your `proxy_pass` statement, which means every request is changed to `/` before passing upstream. You probably want to use `rewrite ^/dev(.*)$ $1 break;` if you need to remove the `/dev` prefix from the requested URI. – Richard Smith Nov 17 '20 at 09:40
  • I updated my question with a working config (which requires my upstream to be up before nginx). Since `proxy_pass http://front-dev:4200/;` works, I assumed `proxy_pass http://$frontdev:4200/;` also works as soon as `$frontdev` is set to `front-dev;`. – arthur.sw Nov 18 '20 at 13:45
  • Bad assumption. See [this document](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass), particularly the part "When variables are used ..." – Richard Smith Nov 18 '20 at 13:49
  • @RichardSmith -- this really should be the answer rather than a comment. I found that using a variable in `proxy_pass` caused that directive to not work as expected even with a trailing slash. Adding in the `rewrite` and removing the trailing `/` from `proxy_pass` fixed my issue – Neil C. Obremski Apr 09 '21 at 23:48

0 Answers0