O
O
Oleg2018-09-19 12:32:39
Nginx
Oleg, 2018-09-19 12:32:39

Setting up routing and 404 nginx?

The server has nginx installed.
If you enter domain.com/ru/something on the site, the site will return its 404 page if there is no such link.
And if you add .php - domain.com/ru/something.php - knocks out nginx 404. How to get rid of this? So that the site handles the 404 error, not Nginx.
Now this is the config:

server {
server_name domain.com;
root /var/www/domain.com;
index index.php index.html;

listen 80;
listen [::]:80;
listen 443 default ssl;

ssl_certificate      /etc/ssl/domain.com.crt;
ssl_certificate_key  /etc/ssl/domain.com.key;

# Redirect HTTP to HTTPS
if ($scheme = http) {
    return 301 https://$server_name$request_uri;
}

access_log /var/log/nginx/myopencart_access.log;

location = /sitemap.xml {
  return 301 /index.php?route=extension/feed/google_sitemap;
}

location = /googlebase.xml {
  return 301 /index.php?route=extension/feed/google_base;
}

location /image/data {
    autoindex on;
}
location /admin {
    index index.php;
}
location / {
    try_files $uri @opencart;
}
location @opencart {
    rewrite ^/(.+)$ /index.php?_route_=$1 last;
}

# Make sure files with the following extensions do not get loaded by nginx because nginx would display the source code, and these files can contain PASSWORDS!
location ~* \.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$|\.php_ {
    deny all;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
    deny all;
    access_log off;
    log_not_found off;
}
location ~*  \.(jpg|jpeg|png|gif|css|js|ico|json|xml|woff|woff2)$ {
    expires max;
    add_header Cache-Control "max-age=2592000";
    access_log off;
}
location ~ \.php$ {
            try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_intercept_errors off;
    fastcgi_buffer_size 16k;
    fastcgi_buffers 4 16k;
}
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dodo512, 2018-09-19
@bysobi

location ~ \.php$ {
       try_files $uri =404;

Replaced by:
location ~ ^/(.*\.php)$ {
        try_files $uri /index.php?_route_=$1;

A
Alexey Arkh, 2018-09-19
@AlekseyArh

error_page 404 /404.html;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question