nginx rewrite location to one served locally












0















I have two SPA-applications in two different languages.
index.html is the same, the only difference is in phrases built in js code. I use nginx to serve them basing on cookie



It may be served from folder as built and bundled app or from localhost(docker in my case, but I guess it doesn't matter)



When app is built and bundled, there is no problem – everything works with cookie or without.
But as soon I want to serve english version, rewriting to localhost port fails. It loads correct index.html, but all static files fail with 404.



How can I fix this?



UPD: it works fine with redirect instead of rewrite and different base-href. But it is not a nice solution(



My config looks like



 location ^~ /app {
if ($http_cookie ~* "locale_en") {
rewrite ^(.*)/app(.*)$ $1/app-en$2 last;
}
alias /var/www/myproject/dist/apps/myapp;
try_files $uri $uri/ /index.html?$query_string @app-site;
}


location ^~ /app-en {
alias /var/www/myproject/dist/apps/myapp-en;
try_files $uri $uri/ /index.html?$query_string @app-site-en;
}


location @app-site {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://host.docker.internal:42031;
}

location @app-site-en {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://host.docker.internal:42030;
}


index.html of both apps looks like



<!doctype html>
<html>
<head>
<base href="/app/">
<link rel="stylesheet" href="styles.css"></head>
<body>
<app-root></app-root>
<script type="text/javascript" src="main.js"></script>
</body>
</html>









share|improve this question

























  • static files also send cookies, so second location would apply internal alias for statics

    – Deadooshka
    Dec 28 '18 at 13:26











  • it is ok, because static is stored on the same port locally

    – Katerina Pavlenko
    Dec 28 '18 at 13:28













  • i have found that if i use base-href as app-en, it works ok, but cant figure why

    – Katerina Pavlenko
    Dec 28 '18 at 13:29











  • you could trace your 404s with detailed error_log

    – Deadooshka
    Dec 28 '18 at 14:15
















0















I have two SPA-applications in two different languages.
index.html is the same, the only difference is in phrases built in js code. I use nginx to serve them basing on cookie



It may be served from folder as built and bundled app or from localhost(docker in my case, but I guess it doesn't matter)



When app is built and bundled, there is no problem – everything works with cookie or without.
But as soon I want to serve english version, rewriting to localhost port fails. It loads correct index.html, but all static files fail with 404.



How can I fix this?



UPD: it works fine with redirect instead of rewrite and different base-href. But it is not a nice solution(



My config looks like



 location ^~ /app {
if ($http_cookie ~* "locale_en") {
rewrite ^(.*)/app(.*)$ $1/app-en$2 last;
}
alias /var/www/myproject/dist/apps/myapp;
try_files $uri $uri/ /index.html?$query_string @app-site;
}


location ^~ /app-en {
alias /var/www/myproject/dist/apps/myapp-en;
try_files $uri $uri/ /index.html?$query_string @app-site-en;
}


location @app-site {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://host.docker.internal:42031;
}

location @app-site-en {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://host.docker.internal:42030;
}


index.html of both apps looks like



<!doctype html>
<html>
<head>
<base href="/app/">
<link rel="stylesheet" href="styles.css"></head>
<body>
<app-root></app-root>
<script type="text/javascript" src="main.js"></script>
</body>
</html>









share|improve this question

























  • static files also send cookies, so second location would apply internal alias for statics

    – Deadooshka
    Dec 28 '18 at 13:26











  • it is ok, because static is stored on the same port locally

    – Katerina Pavlenko
    Dec 28 '18 at 13:28













  • i have found that if i use base-href as app-en, it works ok, but cant figure why

    – Katerina Pavlenko
    Dec 28 '18 at 13:29











  • you could trace your 404s with detailed error_log

    – Deadooshka
    Dec 28 '18 at 14:15














0












0








0








I have two SPA-applications in two different languages.
index.html is the same, the only difference is in phrases built in js code. I use nginx to serve them basing on cookie



It may be served from folder as built and bundled app or from localhost(docker in my case, but I guess it doesn't matter)



When app is built and bundled, there is no problem – everything works with cookie or without.
But as soon I want to serve english version, rewriting to localhost port fails. It loads correct index.html, but all static files fail with 404.



How can I fix this?



UPD: it works fine with redirect instead of rewrite and different base-href. But it is not a nice solution(



My config looks like



 location ^~ /app {
if ($http_cookie ~* "locale_en") {
rewrite ^(.*)/app(.*)$ $1/app-en$2 last;
}
alias /var/www/myproject/dist/apps/myapp;
try_files $uri $uri/ /index.html?$query_string @app-site;
}


location ^~ /app-en {
alias /var/www/myproject/dist/apps/myapp-en;
try_files $uri $uri/ /index.html?$query_string @app-site-en;
}


location @app-site {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://host.docker.internal:42031;
}

location @app-site-en {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://host.docker.internal:42030;
}


index.html of both apps looks like



<!doctype html>
<html>
<head>
<base href="/app/">
<link rel="stylesheet" href="styles.css"></head>
<body>
<app-root></app-root>
<script type="text/javascript" src="main.js"></script>
</body>
</html>









share|improve this question
















I have two SPA-applications in two different languages.
index.html is the same, the only difference is in phrases built in js code. I use nginx to serve them basing on cookie



It may be served from folder as built and bundled app or from localhost(docker in my case, but I guess it doesn't matter)



When app is built and bundled, there is no problem – everything works with cookie or without.
But as soon I want to serve english version, rewriting to localhost port fails. It loads correct index.html, but all static files fail with 404.



How can I fix this?



UPD: it works fine with redirect instead of rewrite and different base-href. But it is not a nice solution(



My config looks like



 location ^~ /app {
if ($http_cookie ~* "locale_en") {
rewrite ^(.*)/app(.*)$ $1/app-en$2 last;
}
alias /var/www/myproject/dist/apps/myapp;
try_files $uri $uri/ /index.html?$query_string @app-site;
}


location ^~ /app-en {
alias /var/www/myproject/dist/apps/myapp-en;
try_files $uri $uri/ /index.html?$query_string @app-site-en;
}


location @app-site {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://host.docker.internal:42031;
}

location @app-site-en {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://host.docker.internal:42030;
}


index.html of both apps looks like



<!doctype html>
<html>
<head>
<base href="/app/">
<link rel="stylesheet" href="styles.css"></head>
<body>
<app-root></app-root>
<script type="text/javascript" src="main.js"></script>
</body>
</html>






nginx single-page-application nginx-location






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 13:36







Katerina Pavlenko

















asked Dec 28 '18 at 12:44









Katerina PavlenkoKaterina Pavlenko

1,1821620




1,1821620













  • static files also send cookies, so second location would apply internal alias for statics

    – Deadooshka
    Dec 28 '18 at 13:26











  • it is ok, because static is stored on the same port locally

    – Katerina Pavlenko
    Dec 28 '18 at 13:28













  • i have found that if i use base-href as app-en, it works ok, but cant figure why

    – Katerina Pavlenko
    Dec 28 '18 at 13:29











  • you could trace your 404s with detailed error_log

    – Deadooshka
    Dec 28 '18 at 14:15



















  • static files also send cookies, so second location would apply internal alias for statics

    – Deadooshka
    Dec 28 '18 at 13:26











  • it is ok, because static is stored on the same port locally

    – Katerina Pavlenko
    Dec 28 '18 at 13:28













  • i have found that if i use base-href as app-en, it works ok, but cant figure why

    – Katerina Pavlenko
    Dec 28 '18 at 13:29











  • you could trace your 404s with detailed error_log

    – Deadooshka
    Dec 28 '18 at 14:15

















static files also send cookies, so second location would apply internal alias for statics

– Deadooshka
Dec 28 '18 at 13:26





static files also send cookies, so second location would apply internal alias for statics

– Deadooshka
Dec 28 '18 at 13:26













it is ok, because static is stored on the same port locally

– Katerina Pavlenko
Dec 28 '18 at 13:28







it is ok, because static is stored on the same port locally

– Katerina Pavlenko
Dec 28 '18 at 13:28















i have found that if i use base-href as app-en, it works ok, but cant figure why

– Katerina Pavlenko
Dec 28 '18 at 13:29





i have found that if i use base-href as app-en, it works ok, but cant figure why

– Katerina Pavlenko
Dec 28 '18 at 13:29













you could trace your 404s with detailed error_log

– Deadooshka
Dec 28 '18 at 14:15





you could trace your 404s with detailed error_log

– Deadooshka
Dec 28 '18 at 14:15












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53958830%2fnginx-rewrite-location-to-one-served-locally%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53958830%2fnginx-rewrite-location-to-one-served-locally%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas