M
M
Magic_Moment2021-10-14 17:49:32
Nginx
Magic_Moment, 2021-10-14 17:49:32

How to fix a 404 error for a bilingual site in NGINX?

Hello.
When migrating a site (MODx engine) from Apache to NGINX, I encountered the fact that the URLs for the English version of the site give a 404 error, while everything works for the Russian one.

URLs for the Russian version look like https://site.com/page/ (server response code 200)
URLs for the English version look like https://site.com/en/page/ (server response code 400)
How to set up URL generation for the English version in the NGINX virthost configs?

Wirthhost config

server {
        listen 80;
        server_name example.com www.example.com;
        root /home/sites/example.com;
        index index.php;
        client_max_body_size 30M;
        location / {
                root /home/sites/example.com;
                if (!-e $request_filename) {
                        rewrite ^/(.*)$ /index.php?q=$1 last;
                }
        }
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(.*)$;
                fastcgi_pass  unix:/run/php/php7.2-fpm.sock
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include fastcgi_params;
                fastcgi_ignore_client_abort on;
                fastcgi_param  SERVER_NAME $http_host;
        }

        location ~ /\.ht {
                deny  all;
        }
}

Partially solved the problem using the code

location ~* ^/(ru|en) {
rewrite ^/(ru|en)?/?(.*)$ /index.php?cultureKey=$1&q=$2 last;
}
The 404 error disappeared, but now all styles for the second language version want to be loaded via /en/style.css, but they are not in that folder and never have been :(

If anyone knows how to help, I will be grateful.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Mezhuev, 2021-10-14
@mezhuev

location / {
    rewrite ^/(en|ru)/?(.*)$ /index.php?cultureKey=$1&q=/$2 last;
    try_files $uri /index.php?q=$request_uri;
}

If the presence index.phpis implied not only in the root, but also in subdirectories, replace $uriwith $uri $uri/.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question