Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx with angular 4

Tags:

nginx

angular

i'm having trouble for my config here is how i want to work mydomain.com -> redirect to correct language : mydomain.com/en/ or mydomain.com/fr/

I have two angular build with i18n, one for each language. the redirection with language works, but direct links with angular 2 not : if i go to mydomain.com/fr/connect -> 404

Here is my nginx configuration

map $http_accept_language $lang {
   default en;
   ~*^fr fr;
}

server {
    listen 80;
    server_name domain.com www.domain.com;
    root /usr/share/nginx/html;
    index  index.html index.htm;
    location = / {
        rewrite "^.$" /$lang/ break;
    }
    location = /$lang/ {
        try_files $uri $uri/ /index.html;
    }
}

if some of you know this, i'm just stuck there, i'm a nginx begginner :/

like image 241
mcfly Avatar asked Jul 30 '26 12:07

mcfly


1 Answers

The second location block is wrong. You probably need everything to be directed to index.html (other than resource files). You might try something like this:

map $http_accept_language $lang {
    default en;
    ~*^fr fr;
}

server {
    listen 80;
    server_name domain.com www.domain.com;
    root /usr/share/nginx/html;
    index  index.html index.htm;
    location = / {
        return 302 /$lang/;
    }
    location / {
        try_files $uri $uri/ /index.html;
    }
}
like image 160
Richard Smith Avatar answered Aug 01 '26 06:08

Richard Smith