skip to Main Content

We have a website where we are using a Laravel for the part available to the end-user, and an older legacy codebase for the admin panel.

Currently root is the public folder for the currently deployed version.

The website folder structure looks something like this:

admin/
  |____index.php <-- desired entrypoint for admin-related requests
app/

public/
  |____index.php <-- main entrypoint for website

resources/

routes/

So, when someone wants to access the admin panel they go to example.com/admin

Here’s the current nginx configs file we are using.

example.com

server {
    server_name example.com;
    root /var/www/example.com/current/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location /admin {
        proxy_pass http://localhost:3000;
    }

    error_page 404 /index.php;
    
    location ~ .php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
    
    location ~ /.(?!well-known).* {
        deny all;
    }
}

example.com/admin

server {
    listen 3000;
    server_name example.com;
    root /var/www/example.com/current/admin;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ .php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /.(?!well-known).* {
        deny all;
    }
}

I’m not very well versed in nginx server configuration and would love som assistance on this.

EDIT 1

Config file using map:

EDIT 2

Updated the config below with the changes made to make it work.

map $uri $siteroot {
    # This didn't work, per the accepted answer.
    # ^/admin  /var/www/example.com/current/admin;

    # This works great!
    ^/admin   /var/www/example.com/current;
    default   /var/www/example.com/current/public;
}

server {
    server_name example.com;
    root $siteroot;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    error_page 404 /index.php;

    location ~ .php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /.(?!well-known).* {
        deny all;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    server_name example.com;
    return 404; # managed by Certbot
}

2

Answers


  1. I would use map directive for this, something like

    map $uri $siteroot {
        ~^/admin  /var/www/example.com/current/admin;
        default   /var/www/example.com/current/public;
    }
    
    server {
        server_name example.com;
        root $siteroot;
        ...
    }
    
    Login or Signup to reply.
  2. If you try changing root directory, Nginx will shows not found error.

    Because, Nginx tries find file with request path even you changed root.

    For example, If you change root directory for /admin path to /app/admin, Nginx will find file in /app/admin/admin. That’s why you can’t reach file.

    Try insert rewrite /admin(.*) $1 break; line at /admin location block and set root path, or use alias expression.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search