m4l490n
m4l490n

Reputation: 1692

How should I configure nginx to serve both a web page and gitweb?

I have installed nginx in my server and it is serving a Hello World website using SSL. I have also installed gitweb and I have configured it as shown in the setting up Nginx for serving Git repositories over HTTP using Gitweb tutorial and it is working fine on port 4321. I can access my Hello World website with www.my-website.com and gitweb with www.my-website.com:4321 having two nginx sites-enabled:

my-website

server {
    # SSL configuration
    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/my-website.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/my-website.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    # Server
    server_name my-website.com www.my-website.com;

    # Landing Page
    root /var/www/my-website.com/html;

    # Basic Authentication
    auth_basic "Restricted Content";
    auth_basic_user_file /etc/nginx/.htpasswd;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }
}

server {
    if ($host = www.my-website.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = my-website.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80 default_server;
    listen [::]:80 default_server;

    server_name my-website.com www.my-website.com;
    return 404; # managed by Certbot
}

And gitweb

server {
    # Git repos are browsable at http://my-website.com:4321/
    listen 4321;

    # Basic Authentication
    auth_basic "Restricted Content";
    auth_basic_user_file /etc/nginx/.htpasswd;

    location /index.cgi {
        root /usr/share/gitweb/;
        include fastcgi_params;
        gzip off;
        fastcgi_param SCRIPT_NAME $uri;
        fastcgi_param GITWEB_CONFIG /etc/gitweb.conf;
        fastcgi_pass  unix:/var/run/fcgiwrap.socket;
    }

    location / {
        root /usr/share/gitweb/;
        index index.cgi;
    }
}

I want to configure nginx to access the Hello World website as I normally do, with www.my-website.com, and gitweb with www.my-website.com/git but I haven't been able to do that.

This question, How to serve GIT through HTTP via NGINX with user/password?, is almost perfect. The problem is that in there it is explained how to substitute the landing page by gitweb. If I configure nginx with the information from the answer to that question, then I can access gitweb just fine, as well as all individual projects, but I lose the Hello World page that I also need.

I then learned about Reverse Proxying and try to combine what I have with the setting up Nginx for serving Git repositories over HTTP using Gitweb tutorial to have the following nginx configuration:

server {
    # SSL configuration
    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/my-website.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/my-website.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    # Server
    server_name my-website.com www.my-website.com;

    # Landing Page
    root /var/www/my-website.com/html;

    # Basic Authentication
    auth_basic "Restricted Content";
    auth_basic_user_file /etc/nginx/.htpasswd;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # Attempt to access gitweb without the port number
    location /git {
        proxy_pass http://localhost:4321/;
    }
}

server {
    if ($host = www.my-website.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = my-website.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80 default_server;
    listen [::]:80 default_server;

    server_name my-website.com www.my-website.com;
    return 404; # managed by Certbot
}

That helped a little. Now the main gitweb page is shown when I enter www.my-website.com/git

enter image description here

BUT when I click on any of my projects, instead of opening them, I get this:

enter image description here

I have looked into other options like the NGINX Configuration for Gitweb and git-http-backend tutorial but that also substitutes the Hello World web page and also is meant to configure to have HTTPS access to the repos which is something I definitely don't want. I clone them through ssh with PKI. Gitweb access is only for visualization, not cloning.

I also tried with Configure nginx to serve two websites which seem promising since the question is similar to mine, but I get a 404 Not Found when substituting location /git { with:

location /git {
    root /usr/share/gitweb/;
    index index.cgi;

    try_files $uri $uri/ =404;
}

I think my "combined approach", the one mentioned before is the right way to go, or is it? Should I add, remove, or modify something to not get the about:blank#blocked? or am I completely lost?

Upvotes: 1

Views: 888

Answers (1)

mgsxman
mgsxman

Reputation: 776

Try this:

server {
    # SSL configuration
    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/my-website.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/my-website.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    # Server
    server_name my-website.com www.my-website.com;

    location / {
        # Landing Page
        root /var/www/my-website.com/html;

        # Basic Authentication
        auth_basic "Restricted Content";
        auth_basic_user_file /etc/nginx/.htpasswd;

        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }
    location /git/ {
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Host $http_host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_http_version 1.1;

        proxy_pass http://127.0.0.1:4321/;
    }
}

server {
    if ($host = www.my-website.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = my-website.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80 default_server;
    listen [::]:80 default_server;

    server_name my-website.com www.my-website.com;
    return 404; # managed by Certbot
}

Upvotes: 0

Related Questions