[Nginx][Gogs] Serving gogs through nginx

2019-09-10 12:24发布

问题:

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

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