1

i am trying to implement a nginx server as reverse proxy for accessing my Gogs instance. Reason is i need to access my services from work, where all but standard ports are blocked. To avoid port conflicts on my server, most serves are running on ports >1000 (and, for that matter, Gogs too, on default 3000). So i created my own vhost config to redirect to Gogs. I get the plain html site, but errors showing errors on loading images and scripts. It seems as if the Gogs itself redirect the clients to multiple subressources, for example /js, /img, /asset and /user. I then added the /js and /img paths as location to my nginx config and got the site running. However, this seems to quite a lot of work, keeping all those paths tracked and configured. Is there a way for me to serve those paths to the client via nginx without having to configure them one by one?

The Gogs and nginx instance are running on the same server, the redirect is configured via ip, no loopback like localhost or 127.0.0.1 used, even though i tried that without success.

Thanks in advance for your help and find my config below. RMG P.S: I checked different tutorials and questions, including this stackoverflow question

server {
        listen 80 default_server;
        server_name devsrv;
        
        #this redirect to another server on port 80 works fine
        location /nextcloud {
                proxy_pass http://OTHERIP/nextcloud;
        }

        location /gogs/ {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_pass http://LOCALIP:3000/;
        }
        # gogs script location
        location /js {
                proxy_pass http://LOCALIP:3000/js;
        }
        # gogs image location
         location /img {
                proxy_pass http://LOCALIP:3000/img;
        }

}
Tim
  • 33
  • 1
  • 8

1 Answers1

1
server {
        listen 80 default_server;
        server_name devsrv;

        #this redirect to another server on port 80 works fine
        location /nextcloud {
                proxy_pass http://OTHERIP/nextcloud;
        }

        location /gogs/ {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_pass http://LOCALIP:3000/;
        }
        location / {
                proxy_pass http://LOCALIP:3000$request_uri;
        }


}

Set all the specific url that need to be handled with special params (X-Real-IP on /gogs or/nextcloud to other ip) and send all the others to gogip with $request_uri.

/js? LOCALIP:3000/js

/img ? LOCALIP:3000/img

Everything will go to it's own path that arrived.

If you want to send only the connections that come from gogs to the REALIP (I will suppose those redirects sent from gogs come from the REALIP ), you can make another bracket set, so you can:

server {
        listen REALIP:80 default_server;
        server_name devsrv;

        #this redirect to another server on port 80 works fine
        location / {
                proxy_pass http://REALIP:3000$request_uri;
        }
}

Hope I helped.

flaixman
  • 702
  • 6
  • 14
  • Hi @flaixman, thanks for your help, your solution works. I only implemented the first part, but it is enough to successfully pull from the repo. Right now, the default-site (location /) is redirected to Gogs. I understand that the second part would fix that by only using that location for redirects from Gogs, is that right? – Tim Nov 30 '18 at 17:35
  • The second part is only if you want to handle the connection from Gogs in a special way (if someone enter that /subpath your nginx won't handle it there, only if it comes from Gogs). The second part only matters if you really want to check from where the connection comes, if those paths can be accesses from "outside", you don't really need to put it. – flaixman Dec 03 '18 at 15:08