Search code examples
nginxwebserverphabricator

How to redirect nginx request to different path


Here is my nginx.conf which serves any request coming to example.com with /srv/phabricator/phabricator/webroot/index.php. I want to change the functionality such that if a request comes in to example.com/test then /home/phragile/public/index.php is served.

daemon off;
error_log stderr info;
worker_processes  1;
pid        /run/nginx.pid;

events {
    worker_connections  4096;
    use epoll;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;
    client_max_body_size  200M;
    client_body_buffer_size 200M;

    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    upstream websocket_pool {
        ip_hash;
        server 127.0.0.1:22280;
    }

    server {
        listen       *:80;

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        root /srv/phabricator/phabricator/webroot;
        try_files $uri $uri/ /index.php;

        location /.well-known/ {
            root /srv/letsencrypt-webroot;
        }

        location / {
            index index.php;

            if ( !-f $request_filename )
            {
                rewrite ^/(.*)$ /index.php?__path__=/$1 last;
                break;
            }
        }

        location /index.php {
            include /app/fastcgi.conf;
            fastcgi_param PATH "/usr/local/bin:/usr/bin:/sbin:/usr/sbin:/bin";
            fastcgi_pass 127.0.0.1:9000;
        }

        location = /ws/ {
            proxy_pass http://websocket_pool;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_read_timeout 999999999;
        }
    }
}

I have tried the following but it does not work:

location /test {
     root /home/phragile/public;
}

Can someone show me what needs to be added in the .conf file?


Solution

  • You will need to use alias rather than root as you are attempting to map /test to /home/phragile/public and the latter does not end with the former. See this document for more. You will also need to execute PHP within that location (see your location /index.php block).

    You have a very specific configuration that is designed to execute just one PHP file. A general solution for /test might look like:

    location ^~ /test {
        alias /home/phragile/public;
        if (!-e $request_filename) { rewrite ^ /test/index.php last; }
    
        location ~ \.php$ {
            if (!-f $request_filename) { return 404; }
    
            include /app/fastcgi.conf;
            fastcgi_param PATH "/usr/local/bin:/usr/bin:/sbin:/usr/sbin:/bin";
            fastcgi_pass 127.0.0.1:9000;
    
            fastcgi_param  SCRIPT_FILENAME $request_filename;
        }
    }
    

    I have pasted your existing FastCGI directives (which I presume is working for you) and added the required value for SCRIPT_FILENAME (assuming you are using php_fpm or similar).

    Of course, if there is no static content under /test you can simplify considerably.