0

I'm running through an issue setting up Gogs through Nginx on my raspberry.

I just want to be able to redirect http://raspberry-ip-address:3000 to http://raspberry-ip-address/gogs.

Below my nginx virtualhost conf :

server {
    listen 80;
    server_name localhost;

    location /gogs/ {
        proxy_pass http://localhost:3000;
    }
}

When I go on http:// raspberry-ip-address:3000, I get the installation page from gogs -> so Gogs is runnning well.

When I go on http:// raspberry-ip-address/gogs, I got a 404 Not found error. however the log from Gogs is somehow "reacting" because I get :

[Macaron] 2016-08-24 14:40:30: Started GET /gogs/ for 127.0.0.1
[Macaron] 2016-08-24 14:40:30: Completed /gogs/ 302 Found in 1.795306ms
2016/08/24 14:40:30 [D] Session ID: 8e0bbb6ab5478dde
2016/08/24 14:40:30 [D] CSRF Token: YfL58XxZUDgwim9qBCosC7EXIGM6MTQ3MTk4MDMxMzMxMTQ3MjgzOQ==

For more information here is my nginx/error.log :

request: "GET /localhost HTTP/1.1", host: "192.168.1.15"
2016/08/24 14:40:30 [error] 3191#0: *4 open() "/usr/share/nginx/html/install" failed (2: No such file or directory), client: 192.168.1.12, server: localhost, request: "GET /install HTTP/1.1", host: "192.168.1.15"

It seems to me that Nginx is not redirecting correctly the request. Any idea ?

Thanks ;)

GrowNarko
  • 17
  • 6
  • Does [this post](https://serverfault.com/questions/379675/nginx-reverse-proxy-url-rewrite) help you? – syntonym Aug 24 '16 at 12:55
  • Not really, I tried every answer from this post. The set up is working if my nginx conf is like the following : server { listen 80; server_name localhost; location / { proxy_pass http://localhost:3000; } } – GrowNarko Aug 24 '16 at 13:13
  • So the rewrite does not rewrite the URL? Or does the rewrite happen but somehow gog still gets `/gogs/`? Does it get `/gogs/` even if you put it somewhere else like `/git/`? – syntonym Aug 24 '16 at 13:27
  • Also the `proxy_pass http://localhost:3000/;` did also not work? – syntonym Aug 24 '16 at 13:28
  • After multiple, I found the solution. `return 301 http://localhost:3000;` – GrowNarko Aug 25 '16 at 05:49
  • I used this link to help me https://www.nginx.com/blog/creating-nginx-rewrite-rules/ – GrowNarko Aug 25 '16 at 05:50
  • That should only work from the machine where gogs runs, it tells the browser to ask again at `localhost:3000`, but localhost is of course different depending on which machine you are asking from. Only the raspberrypi should find there a running gogs. – syntonym Aug 25 '16 at 06:50

1 Answers1

1

For me the following config works:

location /gogs/ {
    proxy_pass http://localhost:3000/;
}

but the following (what you posted) produces the error you mentioned:

location /gogs/ {
    proxy_pass http://localhost:3000;
}

note the / and the and of the url.

A HTTP redirect (30x) does not solve the problem, because it will redirect to localhost which is not the raspberry pi but the computer that does the request.

Complete nginx conf in /etc/nginx/nginx.conf:

user nginx;
worker_processes  1;

events {
    worker_connections  1024;
}


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

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;


        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        location /git/ {
            proxy_pass http://127.0.0.1:3333/;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }   
}
syntonym
  • 7,134
  • 2
  • 32
  • 45