Home > database >  Keeping nginx location sub-directories & sub-files under the origional location directory
Keeping nginx location sub-directories & sub-files under the origional location directory

Time:11-11

I have a location directive

location /nextcloud {
    proxy_pass http://nextcloud_container:80/;
}

When I visit http://192.168.40.231:408/nextcloud, the page loads fine because it passing through to my nextcloud docker address http://192.168.1.2:80 (just an example container address).

The problem is that it's missing all css files because the requests for those files are going to http://192.168.40.231:408/style.css instead of http://192.168.1.2:80/style.css (just an example container address). This works the same for redirects to other pages example) /page2.html

So what I would like to happen is:

  • When I visit /nextcloud, all files and page redirects that the nextcloud container does, should append to the end of the /nextcloud, example) nextcloud/style.css. So that the style.css file is passed through to the container instead of the host computer address.
  • There are other location directives that I would like to do the same thing

nginx.conf

user nginx;
worker_processes  1;


events {
    worker_connections  1024;
}


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

    sendfile        on;

    keepalive_timeout  65;

    gzip on;

    proxy_buffering off;
    proxy_cache off;
    proxy_http_version 1.1;


    server {
        listen 80;
        server_name nginx_container;

        location /nextcloud {
            proxy_pass http://nextcloud_container:80/;
        }
        location /syncthing {
            proxy_pass http://syncthing_container:8384/;
        }
    }
}

docker-compose.yml

version: "3"
services:
  nginx:
    image: nginx
    build: ./nginx
    container_name: nginx_container
    ports:
      - 408:80
    networks:
      - primary_network

  nextcloud:
    container_name: nextcloud_container
    image: nextcloud
    build: ./nextcloud
    networks:
      - primary_network

  syncthing:
    container_name: syncthing_container
    image: syncthing/syncthing
    build: ./syncthing
    ports:
      - 22000:22000/tcp # TCP file transfers
      - 22000:22000/udp # QUIC file transfers
      - 21027:21027/udp # Receive local discovery broadcasts
    restart: unless-stopped
    networks:
      - primary_network

networks:
  primary_network:
    ipam:
      driver: default

This is a similar question I found, incase this one is easier to understand How can I use Nginx as a proxy for Nextcloud when the URL subdirectories are changing?

CodePudding user response:

I figured out a solution to this, you just have to map each service to a new server block with a unique port. Here's some code to better show what I mean.

user nginx;
worker_processes  1;


events {
    worker_connections  1024;
}


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

    sendfile        on;

    keepalive_timeout  65;

    gzip on;

    proxy_buffering off;
    proxy_cache off;
    proxy_http_version 1.1;

    # Redirects http traffic to https blocks below
    server {
        listen 80;

        server_name xx.xx.xx.xx;

        return 302 https://$server_name$request_uri;
    }


    # SSL Configurations
    include /etc/nginx/nginx_ssl.conf;

    # NextCloud
    server {
        listen 443 ssl;
        server_name xx.xx.xx.xx;

        # Reverse proxy to syncthing container
        location / {
            proxy_pass http://xx.xx.xx.xx:1112/;
        }
        # Redirects to unique syncthing port
        location /syncthing {
            return 301 https://xx.xx.xx.xx:444/;
        }
    }
    # Syncthing
    server {
        listen 444 ssl;
        server_name xx.xx.xx.xx;

        # Reverse proxy to syncthing container
        location / {
            proxy_pass https://xx.xx.xx.xx:1114/;
        }
        # Redirects to unique nextcloud port
        location /nextcloud {
            return 301 https://xx.xx.xx.xx:443/;
        }

    }
}
  • Related