I built an angular-5 application using i18n that supports both french and english. I then deployed a separate version of the app for each supported language
- dist |___ en/ | |__ index.html |___ fr/ |__ index.html
I also added the following nginx configuration to serve the application in both languages;
server { root /var/www/dist; index index.html index.htm; server_name host.local; location ^/(fr|en)/(.*)$ { try_files $2 $2/ /$1/index.html; } }
What I wanted to do is to serve both applications and allow switching between the english and the french version.
Let's say for example I'm on host.local/en/something
if I switch to host.local/fr/something
I should get the french version of the "something" page.
With the nginx configuration I shared, I get a 404 not found error every time I refresh pages when browing my apps which also prevents me from browsing my apps independently from one another and switching between them.
What did I miss? What's the appropriate Nginx conf to achieve that?
i've done the same thing on iis, first you have to build your app with "base-href" option:
and for nginx use this config
and for navigation from /en/someroute to /fr/someroute , you can get the current router url in your component where you have the language switcher
and when click on a language selector you redirect to the same route with the selected language :
change language method
There is a common misunderstanding in the way that http://nginx.org/r/try_files works. If you look closer in the docs (from the above link), you'll notice that although the first and intermediate parameters in the
try_files
directive are of type "file", the final one is called the "uri"; which, in your case, once you fix your http://nginx.org/r/location to handle the regexp appropriately (yourlocation
code is missing the~
modifier), may result in an infinite loop, as you confirm in your comments.Note that generally, the regular expressions are not recommended to be used in nginx for maximum performance in simple situations where regex use might as well be avoided, so, I'd recommend you to have two independent locations, one each for English and French.
Note that the above code assumes that you perform correct URL handling from within your webapp frontend itself — if a given resource is shared between the
/en/
and/fr/
versions, then it would be requested directly via the/
base, without a/en/
or/fr/
specifier. The=410
part of the code behaves similarly to how=404
would, except that the error is slightly different to make it easier to debug which directive is responsible for the error.After building with:
copy the builds to nginx serve directory:
Then I found 2 different NGINX configs that work for me:
Using root path
Using alias
Note: In the root path solution you can remove the
autoindex on
option but you'll also have to remove the$uri$args/
part from thetry_files
or else you'll get "directory index of "[directory]" is forbidden error".FYI: You can find useful those nice explanations on ROOT vs ALIAS.
Versions