Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I have a directory/application that is located outside of the web root directory of my site.

Say the site is here:

/var/www/site/htdocs/

And the external app is located here:

/var/www/apps/coolapp/

My question is how can I configure nginx to map/route all requests that are like www.mysite.com/coolapp/* (asterisk being wildcard) to the external location /var/www/apps/coolapp/? For example, www.mysite.com/coolapp/test.php should server /var/www/apps/coolapp/test.php.

Per @krokola's answer, I tried adding the alias directive in the production.conf file that the main nginx.conf file includes. Here is what production.conf currently looks like

server {
    listen 80;
    listen 443 ssl;

    ssl_certificate     /blah/blah/blah;
    ssl_certificate_key /blah/blah/blah;
    ssl_protocols       blah blah blah;
    ssl_ciphers         blahblahblah;
    ssl_prefer_server_ciphers blahblah;

    access_log /var/log/nginx/www.mysite.com-access.log;
    error_log  /var/log/nginx/www.mysite.com-error.log error;

    server_name mysite.com www.mysite.com;
    root /var/www/site/htdocs;

    include conf/magento_rewrites.conf;
    include conf/magento_security.conf;
    include /var/www/site/nginx/*.conf;

    #-------CODE IN QUESTION-------
    location  /coolapp/ {
        alias  /var/www/apps/coolapp/;
        location ~ \.php {
            # Copied from "# PHP Handler" below
            fastcgi_param MAGE_RUN_CODE default;
            fastcgi_param MAGE_RUN_TYPE store;
            fastcgi_param HTTPS $fastcgi_https;
            rewrite_log on;

            # By default, only handle fcgi without caching
            include conf/magento_fcgi.conf;
        }
    }

    # PHP handler
    location ~ \.php {
      ## Catch 404s that try_files miss
      if (!-e $request_filename) { rewrite / /index.php last; }

      ## Store code is defined in administration > Configuration > Manage Stores
      fastcgi_param MAGE_RUN_CODE default;
      fastcgi_param MAGE_RUN_TYPE store;
      fastcgi_param HTTPS $fastcgi_https;
      rewrite_log on;

      # By default, only handle fcgi without caching
      include conf/magento_fcgi.conf;
    }

    # 404s are handled by front controller
    location @magefc {
        rewrite ^(.*) /index.php?$query_string last;
    }

    # Last path match hands to magento or sets global cache-control
    location / {
        ## Maintenance page overrides front controller
        index index.html index.php;
        try_files $uri $uri/ @magefc;
        expires 24h;
    }
}

conf/magento_fcgi.conf looks like this:

fastcgi_pass phpfpm;

## Tell the upstream who is making the request
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;

# Ensure the admin panels have enough time to complete large requests ie: report generation, product import/export
proxy_read_timeout 1600s;

# Ensure PHP knows when we use HTTPS
fastcgi_param  HTTPS           $fastcgi_https;

## Fcgi Settings
include                        fastcgi_params;
fastcgi_connect_timeout        120;
fastcgi_send_timeout           320s;
fastcgi_read_timeout           1600s;
fastcgi_buffer_size            128k;
fastcgi_buffers 512            64k;
fastcgi_busy_buffers_size      128k;
fastcgi_temp_file_write_size   256k;
fastcgi_intercept_errors       off;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME     $fastcgi_script_name;
# nginx will buffer objects to disk that are too large for the buffers above
fastcgi_temp_path              /tmpfs/nginx/tmp 1 2;
#fastcgi_keep_conn              on; # NGINX 1.1.14
expires                        off; ## Do not cache dynamic content
share|improve this question

1 Answer 1

up vote 2 down vote accepted

You need to set an alias.

location  /coolapp/ {
    alias  /var/www/apps/coolapp/;
}

Read more nginx

Update after comment:

Unfortunately, it's not working for me. I'm getting a 404 response when trying to request www.mysite.com/coolapp/test.php when /var/www/apps/coolapp/test.php exists

To understand why alias is not working, you have to take a look at how nginx processes a request: request_processing

From the manual:

Nginx first searches for the most specific prefix location given by literal strings regardless of the listed order. In the configuration above the only prefix location is “/” and since it matches any request it will be used as a last resort. Then nginx checks locations given by regular expression in the order listed in the configuration file. The first matching expression stops the search and nginx will use this location. If no regular expression matches a request, then nginx uses the most specific prefix location found earlier.

If there is a regular expression that matches a request, it overrides static prefixes. In your configuration, when accessing /var/www/apps/coolapp/test.php, 2 locations match:

  • location /coolapp/
  • location ~ .php

Because the second location is a regular expression, is overrides the first. But when you are accessing a html file eg: /var/www/apps/coolapp/test.html, if works as expected as only one location is found, the alias location.

In order to fix the problem with accessing php in alias path and override the default php location, you can add a nested location within the alias location, and do whatever you wish to do when calling php files in the nested location. In your configuration this would be:

 location  /coolapp/ {
     alias  /var/www/apps/coolapp/;
     location ~ \.php {
         Do whatever is needed
     }
 }

You can find an example here

share|improve this answer
    
Awesome. Thank you very much. I'll give it a try later tonight and get back to you. –  Shredder Feb 25 at 23:18
    
Unfortunately, it's not working for me. I'm getting a 404 response when trying to request www.mysite.com/coolapp/test.php when /var/www/apps/coolapp/test.php exists. –  Shredder Feb 26 at 12:02
    
Show us your config please –  krokola Feb 26 at 12:25
    
I just added it to my question. Thanks. –  Shredder Feb 26 at 12:26
    
Alias must work, but you have the " location ~ \.php" which overrides the alias location when calling .php files. Please put a static html file inside /var/www/apps/coolapp/ eg test.html or an image test.jpg and test if that is fetched at mysite.com/coolapp/test.html. –  krokola Feb 26 at 13:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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