reevolt
reevolt

Reputation: 807

nginx config files redirecting to subfolder

I'm currently trying to deploy a website in two directories. Despite the lot of documentations on it, I was not able to obtain the behavior that I wish. This is the architecture of the websites :

I wish configure nginx to obtain this behavior :

currently, my config file contains :

# redirect http to https
server {
    listen 80;
    rewrite ^(.*) https://$host$1 permanent;
}

# main webiste
server {
    listen 443;

    # ssl elements...  
    root   /opt/www/mainsite;
    index index.html;
    server_name             www.mydomain.com;

    location / {
            try_files $uri $uri/ /index.html;
    }
}

On this page I found all the informations to set the config file for CodeIgniter. But I don't know how to create the rules to point mydomain.com/myapp on the CodeIgniter folder and how to configure CodeIgniter in order to set the right configuration.

Is anybody can help me?

thanks in advance

Upvotes: 0

Views: 3254

Answers (2)

Chuan Ma
Chuan Ma

Reputation: 9914

  • You need http://wiki.nginx.org/HttpFastcgiModule to setup CodeIgniter.
  • Using 2 server blocks is better than using if block for redirect. See IF is Evil.
  • Don't use $host because that variable value is obtained from the request's HOST header, and can be easily faked. Always set a server_name directive and use that name instead.
  • Using "return 301" directive is better than a rewrite. Saving cpu time (regex is slow) and easy to follow. Note that a 302 redirect (rewrite...redirect) has side effect because 302 will turn all POST requests to GET requests, which is not good in your case.
  • You don't need try_files in the main site because the main site just serves static files. But you can use 'expires' directive to allow browser to cache the static files.
server {
    listen 80;
    server_name www.mydomain.com;
    return 301 https://$server_name$request_uri;
 }

 server {
    listen 443;
    server_name www.mydomain.com;
    # ssl elements...  

    location / {
        root   /opt/www/mainsite;
        index index.html;
        expires max;
    }

    location /myapp {
        root /opt/www/apps/myapp;
        # fastcgi module goes here...
    }
}

Upvotes: 4

cnst
cnst

Reputation: 27218

server {
    listen 80;
    listen 443 ssl;
    …
    if ($scheme != "https") {
        rewrite ^ https://$server_name$request_uri? redirect;
    }
    root /opt/www/mainsite/;
    location /myapp {
        root /opt/www/apps/myapp/;
    }
}

You'd put whatever configuration that is necessary for your myapp within the location for myapp.

BTW, it is generally a bad idea to host multiple independent apps within a single Host, because of XSS concerns.

Upvotes: 0

Related Questions