Score:0

URL rewriting in nginx for multiple projects php & react

pl flag

I am facing a 404 error when I access my site directly from IP. The default root directory is /var/www/html. And I am accessing it through http://<server_public_ip>/folder_name.

This is my folder structure.

/var/www/html
          ├── WordPress1/
          ├── WordPress2/
          ├── CI_or_Larawel/
          ├── any_direct_file (.php,.zip or anything else)

Generally the default location directive

location / {
    try_files $uri $uri/ =404;
}

should work for all files & folder under the root directory. But its not working. When I access it from the IP http://<server_public_ip>/folder_name its going to 404.

To resolve this I am forced to create multiple location directive based on sites installed on the root directory. I do not want this. I just wanted it should work whatever site/folder I add under root directory.

My conf file is as follows

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

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name _;
    
    location / {
        try_files $uri $uri/ =404;
    }

    # I do not want to add location directive for all folders under /var/www/html 
    # but the default location was not working and it was going to 404. That I would like to change
    location /wordpress1 {
        try_files $uri $uri/ /wordpress1/index.php?$args;
    }

    # pass PHP scripts to FastCGI server
    location ~ \.php$ {
        #try_files $uri =404;
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    #   fastcgi_pass 127.0.0.1:9000;
    }

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

I just want any file/folder I put under /var/www/html it should work normally without adding location directive. Thanks in advance

us flag
I don't see any issue in your configuration why things wouldn't work properly. Please add clear repro steps, what request you are making, what is the expected result and what is the actual result.
Sanjay Goswami avatar
pl flag
I am simply entering the IP/foldername. If you want I can share you the Server name & necessary details privately.
us flag
Please add the exact details into the question. What is the exact request and response.
Sanjay Goswami avatar
pl flag
I have updated my question
us flag
The details are still quite not exact. There is no detailed information what 404 you receive. However, to me your target state looks impossible. You want to support different CMS, but do not want to apply per-CMS configurations. You need to have proper front controller configuration for each CMS, and that requires per path `try_files` directive.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.