H
H
hesy2020-08-12 09:21:25
PHP
hesy, 2020-08-12 09:21:25

404 for routes or how to configure Nginx for subfolders?

There is a directory /var/www/example_com/where the site is located example.com.
From this directory, all routes work correctly, example.com/some_route, example.com/users, etc.

If I place the "second" site in sub directories, that is, I want something to come /var/www/example_com/sandbox/hello/
out of it example.com/sandbox/hello, in which (!) I will redefine the routes, that is, the base path will be /sandbox/helloand all routes will refer to index.phpin this folder, and not root.

Before that, I did this on shared hosting, everything worked as described, on vds I can’t configure the nginx config myself.
By the way, here is the config:

nginx config
server {
  listen 80;
  listen [::]:80;

  root /var/www/example_com;

  index index.html index.php;

  server_name example.com;

  location / {
    try_files $uri $uri/ /index.php$is_args$args;
  }

  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
  }

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


.htaccess
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]


Now, when trying to determine the route in the folder hello, it gives a 404 error.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2020-08-12
@hesy

location /sandbox/hello/ {
  try_files $uri $uri/ /sandbox/hello/index.php$is_args$args;
}

spoiler
server {
  listen 80;
  listen [::]:80;

  root /var/www/example_com;

  index index.html index.php;

  server_name example.com;

  location / {
    try_files $uri $uri/ /index.php$is_args$args;
  }

  location /sandbox/hello/ {
    try_files $uri $uri/ /sandbox/hello/index.php$is_args$args;
  }

  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
  }

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question