Nginx configuration for reverse proxy

I managed to place my nextcloud cloud server and onyoffice document servers behind another reverse proxy server running nginx. I have zero experience on nginx and I am not entirely satisfied with my set-up.

Right now the nextcloud server is running under the /nextloud ROOT directory and I can reach it under http://example.org/nextcloud/. That seems good enough, but my document server is accessible under http://example.org/ which not practical as I would like to set-up another service at that address.

This is my current nginx.conf:

worker_processes 4; # Default 1
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;

    events {
        worker_connections  512; # Default 1024
    }


    http {
            include       mime.types;
            default_type  application/octet-stream;

            keepalive_timeout  65;

            ## Compression
            gzip              on;
            gzip_buffers      16 8k;
            gzip_comp_level   9;
            gzip_http_version 1.1;
            gzip_min_length   10;
            gzip_types        text/plain text/css application/x-javascript text/xml;
            gzip_vary         on;
            gzip_static       on; #Needs compilation with gzip_static support
            gzip_proxied      any;
            gzip_disable      "MSIE [1-6]\.";

    ## Server configuration 
            server {
                    listen       80;
                    server_name example.org;
                    server_name_in_redirect off;
                    access_log      logs/access.log;
                    client_body_buffer_size 1m;
                    proxy_buffering on;
                    proxy_buffer_size 4k;
                    proxy_buffers 8 32k;


    ## proxy the PHP scripts to Apache listening on 127.0.0.1:80
        location /nextcloud {
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass   http://cloudserver:80;
        }
        #location /doc {
        location / {
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_redirect off;
        proxy_pass  http://documentserver:80/;
        }
    }

}

As you may see from the commented line, I tried to redirect the document server to http://example.org/doc/ via

location /doc

but I am getting a 404 Error. Does anyone know how to redirect the document server to another ROOT?

.

Try this on your nextcloud installed machine…
To give Nginx user (www-data) write permission

sudo chown www-data:www-data /usr/share/nginx/nextcloud/ -R

I suppose that I did not explain my question as much as I should have.

  1. I installed NextCloud on a Slackware Linux Server, while I think that you are referring to a Debian/Ubuntu Apache installation. On Slack, NC Server installation is very easy and fool-proof because it is supported by the awesome the https://slackbuilds.org/ community.

  2. My NextCloud server works fine. My concern is only that I failed to redirect my OnlyOffice Document ROOT directory (on a another server - running on Debian) from http://example.org to http://example.org/doc. This is minor inconvenience, but my Document Server works fine and my NC integration also works fine.

  3. I have an Nginx Slackware Reverse Proxy Server (a third server), and despite that I have no experience with nginx, it’s working fine. I should be able to redirect the Debian OnlyOffice Document Server to http://example.org/doc but I have not managed yet.

Thanks for your hint anyway. Naturally my real URL is not http://example.org.

For the sake of any reader that is deploying a separate reverse proxy server, this is my production nginx.conf file for HTTPS that works for me:

worker_processes 4; # Default 1
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;

     events {
         worker_connections  512; # Default 1024
     }

     http {
             include       mime.types;
             default_type  application/octet-stream;

             keepalive_timeout  65;

             ## Compression
             gzip              on;
             gzip_buffers      16 8k;
             gzip_comp_level   9;
             gzip_http_version 1.1;
             gzip_min_length   10;
             gzip_types        text/plain text/css application/x-javascript text/xml;
             gzip_vary         on;
             gzip_static       on; #Needs compilation with gzip_static support
             gzip_proxied      any;
             gzip_disable      "MSIE [1-6]\.";

     ## Server configuration 
             server {
                     listen       443 ssl;
                     server_name example.org
                     ssl on;
                     ssl_certificate      /root/fullchain.pem;
                     ssl_certificate_key  /root/privkey.pem;
                     server_name_in_redirect off;
                     access_log      logs/access.log;
                     # A suitable client_max_body_size has to be set or you will get errors
                     client_max_body_size 10G ;


     ## proxy the PHP scripts to Apache listening on 127.0.0.1:80
         location /nextcloud {
         proxy_set_header X-Forwarded-Host $host;
         proxy_set_header X-Forwarded-Server $host;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_buffering off;
         proxy_pass   https://cloudserver:443;
         }
         location / {
         proxy_set_header X-Forwarded-Host $host;
         proxy_set_header X-Forwarded-Server $host;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_buffering off;
         proxy_pass  https://documentserver:443/;
         }
     }

}