Nginx – Change Nginx document root from /usr/share/nginx to /etc/nginx

amazon-web-servicesnginxredhatweb-server

I've tried many things, STFW, RTFM but I still have this problem. The thing is:

I have a Nnginx installed in an AWS machine (other fella installed it, not me)
and I MUST serve several virtual hosts (*.conf files) which are being pulled from other server (production machine)

My main issue, is that the error handlers path in this .conf files are relatives, for example:

html/errores-prxy/handle404.html

as in

 location = /handle404.html {
     root html/errores-prxy;
}

The problem is that the document root path being called for this handler is /usr/share/nginx, as I can see in the error log:

2015/04/30 10:33:24 [error] 19542#0: *68 open() "/usr/share/nginx/html/errores-prxy/handle404.html" failed (2: No such file or directory), client: 77.240.116.140, server: www.abengoa.com, request: "GET / HTTP/1.1", upstream: "http://172.26.3.9:80/web/", host: "www.abengoa.es"

In this AWS machine, the Nginx is installed in /etc/nginx. And these handler files are in /etc/nginx/html/errores-prxy/handle404.html

So my question would be, how can I make Nginx look in /etc/nginx rather than in /usr/share/nginx/?

I could make a script to change the relative paths to absolute paths in all the *.conf but I'm seeking a more elegant solution as would be changing the Nginx document root.

Many thanks in advance.

Below is my nginx.conf (located in /etc/nginx/nginx.conf, just in case it helps)

user              nginx nginx;
worker_processes  2;
error_log       /var/log/nginx/error.log;
pid             /var/run/nginx.pid;

events {
                worker_connections  4096;
}

http {
                include         /etc/nginx/conf/mime.types;
                default_type    application/octet-stream;
                sendfile        on;
                gzip            on;
                gzip_comp_level 9;
                gzip_min_length 0;
                gzip_proxied    expired no-cache no-store private auth;
                gzip_types      text/plain text/css application/x-javascript application/xml application/javascript;
                set_real_ip_from        192.168.151.3;
                real_ip_header   X-Forwarded-For;
                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_intercept_errors on;
                log_format main '$remote_addr - $remote_user [$time_local] '
                        '"$request" $status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent"' ;
                access_log      /var/log/nginx/access.log main;

                client_max_body_size            50m;
                client_body_buffer_size         4k;
                client_header_buffer_size       2k;
                keepalive_timeout               5 5;
                client_body_timeout             10;
                client_header_timeout           10;
                send_timeout                    10;
                proxy_connect_timeout           5;
                proxy_send_timeout              20;
                proxy_read_timeout              120;
                proxy_buffer_size               8k;
                proxy_buffers                   8 32k;
                proxy_busy_buffers_size         32k;
                proxy_temp_file_write_size      32k;
                server_names_hash_bucket_size   128;
                server_names_hash_max_size      1280;
                server_name_in_redirect         off;
                proxy_cache_path  /etc/nginx/cache  levels=1:2   keys_zone=prxy-cache:100m max_size=256m;
                proxy_cache_key   "$scheme$host$request_uri";
                include /etc/nginx/conf/*.conf;
                include /etc/nginx/conf/sites-enables/*.conf;
}

Best Answer

nginx docs state the following about the --prefix configure command:

--prefix=path — defines a directory that will keep server files. This same directory will also be used for all relative paths set by configure (except for paths to libraries sources) and in the nginx.conf configuration file. It is set to the /usr/local/nginx directory by default.

Thus, all relative paths are computed against this one.

To check the prefix value for the binary you use, check nginx -V.

In your case, I see several options, from best to worst:

  1. Use absolute paths (either directly or prefixing existing paths with a variable). A simple script would make it easy to transform the original paths into propers ones, even launched as a 'post-hook' on file drop by the fellas
  2. Create symbolic links in the computed locations referring to the expected one
  3. Build nginx binaries manually, configuring that command as you see fit