Home > other >  NGINX Two Directories One URL (Codeigniter, HTML)
NGINX Two Directories One URL (Codeigniter, HTML)

Time:11-02

Here is my default.conf file:

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

    root /var/www/html/;
    server_name example.com;

    # Site 01
    location /* {
        alias /var/www/html/home/;
        index index.htm index.php;
        try_files $uri $uri/ /home/index.html;
    }

    # Site 2
    location /shadows {
        alias /var/www/html/shadows/;
        index index.htm index.php;
        try_files $uri $uri/ /shadows/index.php;
    }

    error_page 404 /error/404.php;
    fastcgi_intercept_errors on;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

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

I have a Codeigniter app in

var/www/html/shadows

I also have a HTML site in

var/www/html/home

The issue:

I have been able to make this work for the directories home or shadows but not both.

If I change root to /var/www/html/home the HTML site pops up.

Currently just the shadows codeigniter site pops up with this configuration.

Please let me know your thoughts on resolving this. Thank you very much.

CodePudding user response:

If I understand you correctly, something like this should work:

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

    server_name example.com;

    root /var/www/html/home;

    location /shadows {
        root /var/www/html;
        index index.php;
    }
}

http://example.com/shadows/index.php will resolve to path /var/www/html/shadows/index.php in this case.

CodePudding user response:

You can get the root directive value via the map block:

map $uri $approot {
    ~^/shadows/    /var/www/html;
    default        /var/www/html/home;
}
server {
    listen 80;
    listen [::]:80;

    root $approot;
    server_name example.com;

    # Site 01
    location / {
        index index.htm index.php;
        try_files $uri $uri/ /index.html;
    }

    # Site 2
    location /shadows/ {
        index index.htm index.php;
        try_files $uri $uri/ /shadows/index.php$is_args$args;
    }

    error_page 404 /error/404.php;
    fastcgi_intercept_errors on;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

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

I'm assuming your HTML site should be served under the root prefix /, not the /home/, therefore I changed your try_files $uri $uri/ /home/index.html; directive to the try_files $uri $uri/ /index.html;. I also replaced all your alias directives with root ones. As it said by the alias directive documentation

When location matches the last part of the directive’s value:

location /images/ {
    alias /data/w3/images/;
}

it is better to use the root directive instead:

location /images/ {
    root /data/w3;
}

Additionally there is a well-known bug when you are using alias and try_files directives together, so using the root one instead is more reliable.

  • Related