I have an Ubuntu install on a VPS. I do not have any domain name so all content is accessed via https://IP:port. I have been trying to configure nginx so as to run several webapps accessed via ip/name.

Here is my config

server {

    listen 443 ssl;

    error_log /var/log/nginx/nginx_error.log;

    ssl_certificate /etc/nginx/ssl/server.crt;
    ssl_certificate_key /etc/nginx/ssl/server.key;

    set $root_path '/var/www/html';
    root $root_path;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html index.php;

    server_name localhost;

    # handle static files within project.. break at end to avoid recursive redirect
    location ~ app3/((.*)\.(?:css|cur|js|jpg|jpeg|gif|htc|ico|png|html|xml))$ {
            rewrite app3/((.*)\.(?:css|cur|js|jpg|jpeg|gif|htc|ico|png|html|xml))$ /app3/$1 break;

    location /app3 {
        rewrite ^/app3(.*)?$ /app3/pathto/public/index.php?$1 last;

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

    location /app1 {
        try_files $uri $uri/ /index.php;

    location /app2 {
        try_files $uri/ /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #try_files $uri $uri/ =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_nam

This works well for app1 and app2. App3 (the Laravel app) can't run correctly because it uses absolute urls instead of relative paths. So I get 404 request for /api.. instead of /app3/pathto/public/.. (I have tried a fork of app3 with relative paths and all runs well except some images and fonts not displayed)

How can I configure nginx so that app3 has its own root folder ? A root or alias directive in app3 location would not work properly.

Thanks !


Your question is about giving app3 its own root folder, and that step is easy. As long as you note the pitfalls, you should be alright with this. http://wiki.nginx.org/Pitfalls#Root_inside_Location_Block

However I don't think that will really solve your problem. Your problem seems to be that the Laravel app is unable to work within a sub folder, or that it is simply not configured to run in it. There is not much to do at the nginx level to fix this. But check the config for any help about the app config.

An alternate "solution" is to install (or rewrite) app 3's resources (api/, img/ etc.) in the root, and only rewrite app 1 and 2 instead. It won't be the most elegant solution, but as long as there are no folders called app1 or app2 in the Laravel app it should work.

Edit: A third option, and probably the best, is of course to shell out the $10 or so a year for a domain name, or get a free sub domain from a free dns service. Then you can have a separate domain (or sub domain) for app3. I have no experience with any free dns services, but this should help you.


  • I know giving a location block its own root is easy. But it didn't work either with root or with alias. As you said the problem first come from Laravel app that is not designed to run like this. The alternate solution would work if I only run one Laravel app.. (Which is the case for now but..) I am currently trying this : stackoverflow.com/questions/28531288/… but same problems are rising... I was thinking about your last solution and that seems the best. I have to gather information about free dns service first.
    – Lich4r
    Feb 17 '15 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.