Answer a question

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;
        }

}

Answers

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.

Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐