0

On a server for a small team of people (up to 30) I installed several services which have to be accessible from the browser. I want to use nginx because I consider it being more lightweight.

All services/apps are tested standalone and do their job. But when it comes to putting them all together divided by subfolders (e.g. teamserver.local/service1, or teamserver.local/service2) I struggle badly when it comes to rewrite rules and proxy passing.
I just cant get sub folders configured to work.


Here is my default config:

server {
        listen 80;
        server_name teamserver.local;
        rewrite ^ https://$http_host$request_uri? permanent;  # redirect to https!
    }


server {
    listen 443;
    server_name teamserver.local;

    root /var/www;

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

    ssl_session_timeout 5m;

    ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
    ssl_prefer_server_ciphers on;


    location /service1 {
            fastcgi_pass    127.0.0.1:8000;
            fastcgi_param   SCRIPT_FILENAME     $document_root$fastcgi_script_name;
            fastcgi_param   PATH_INFO           $fastcgi_script_name;

            fastcgi_param   SERVER_PROTOCOL     $server_protocol;
            fastcgi_param   QUERY_STRING        $query_string;
            fastcgi_param   REQUEST_METHOD      $request_method;
            fastcgi_param   CONTENT_TYPE        $content_type;
            fastcgi_param   CONTENT_LENGTH      $content_length;
            fastcgi_param   SERVER_ADDR         $server_addr;
            fastcgi_param   SERVER_PORT         $server_port;
            fastcgi_param   SERVER_NAME         $server_name;
            fastcgi_param   REMOTE_ADDR         $remote_addr;
            fastcgi_param   HTTPS               on;
            fastcgi_param   HTTP_SCHEME         https;
            access_log      /var/log/nginx/seahub.access.log;
            error_log       /var/log/nginx/seahub.error.log;
    }


    location /seafhttp {
            rewrite ^/seafhttp(.*)$ $1 break;
            proxy_pass http://127.0.0.1:8082;
            client_max_body_size 0;
            proxy_connect_timeout  36000s;
            proxy_read_timeout  36000s;
    }

    location /media {
            rewrite ^/media(.*)$ /media$1 break;
            root /home/seafile/seafile/seafile-server-latest/seahub;
    }


    location ~ ^/openproject(/.*|$) {
            alias /home/openproject/openproject/public$1;

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

            passenger_ruby /home/openproject/.rvm/gems/ruby-2.1.4/wrappers/ruby;
            passenger_base_uri /openproject;
            passenger_app_root /home/openproject/openproject;
            passenger_document_root /home/openproject/openproject/public;
            passenger_user openproject;
            passenger_enabled on;
    }


    location /ldap {
            rewrite ^/ldap(.*)$ /$1 break;
            root /usr/share/phpldapadmin/htdocs;
            index index.php index.html index.htm;
            # With php5-fpm:
            #fastcgi_pass unix:/var/run/php5-fpm.sock;
            #fastcgi_index index.php;
            #include fastcgi_params;
    }

    location /musik {
            rewrite /musik(.*) /$1 break;
            proxy_pass http://127.0.0.1:6680;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_redirect off;
    }
}



I am pretty sure that there is something wrong about setting the right root directive and/or maybe the order in which nginx parses the locations. I just don't get it right.

Here it's quite sure, that nginx is searching the root directive. Questions is: WHY?!

I am pretty new to nginx and dont want to keep the messy "every service got it's port. Just use this!" mentality.
As always I'd be happy if someone pointed the finger to the problems. Reading the nginx manual and module reference didnt quite help me although I already figured out some errors and fixed those.. So any help is kindly appreciated.

oliverjkb
  • 614
  • 1
  • 6
  • 10

1 Answers1

2

Use alias instead of root. For example alias /path/to/ldap.

The problem with root directive is that the path after location directive is appended to the end of root directive path.

For example, in your case:

location /ldap {
    root /usr/share/phpldapadmin/htdocs;
}

means that nginx looks for the URL http://server/ldap/index.html at /usr/share/phpldapadmin/htdocs/ldap/index.html location.

With alias /usr/share/phpldapadmin/htdocs;, nginx looks up the same URL at /usr/share/phpldapadmin/htdocs/index.html location.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
  • Good to know! But using alias means I am not allowed to use rewrite any more, so commented it out. This leaves me with 2 new Problems.. :( Firts, I have to type the whole path server.local/ldap/index.php otherwise "No Input file specified". If I entered the whole path, css mime type will be discovered as "text/html" and will therefor not be loaded.. Also subfolders with proxy_pass surely don't work with alias either (no worries, didn't try!) – oliverjkb Jan 30 '15 at 07:49
  • Yays! got the /ldap directory to work now. Took the alias directive, added a "location ~ \.php$" with fastcgi parameters and now it works like a charm! Thanks!! Will post solution as soon as whole task is complete. – oliverjkb Jan 30 '15 at 08:11