【问题标题】:How should I configure nginx to serve both a web page and gitweb?我应该如何配置 nginx 以同时提供网页和 gitweb?
【发布时间】:2020-06-06 23:49:19
【问题描述】:

我已经在我的服务器中安装了 nginx,它正在使用 SSL 为 Hello World 网站提供服务。我还安装了 gitweb,并按照setting up Nginx for serving Git repositories over HTTP using Gitweb 教程中的说明进行了配置,它在端口4321 上运行良好。我可以使用 www.my-website.com 访问我的 Hello World 网站,使用 www.my-website.com:4321 访问我的 gitweb 并启用两个 nginx 站点:

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
}

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

我想像往常一样配置 nginx 以访问 Hello World 网站,使用www.my-website.com,使用www.my-website.com/git 访问 gitweb,但我无法做到。

How to serve GIT through HTTP via NGINX with user/password? 这个问题几乎是完美的。问题是那里解释了 如何替换 gitweb 的登录页面。如果我使用该问题的答案中的信息配置 nginx,那么我可以正常访问 gitweb 以及所有单个项目,但我丢失了我也需要的 Hello World 页面。

然后我了解了 反向代理 并尝试将我所拥有的内容与 设置 Nginx 以使用 Gitweb 通过 HTTP 提供 Git 存储库 教程相结合,以获得以下 nginx 配置:

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
}

这有点帮助。现在当我输入www.my-website.com/git时显示主gitweb页面

但是当我点击我的任何项目而不是打开它们时,我得到了这个:

我已经研究过其他选项,例如 NGINX Configuration for Gitweb and git-http-backend 教程,但它也替代了 Hello World 网页,并且还旨在配置为对 repos 进行 HTTPS 访问这是我绝对不想要的。我通过 ssh 和 PKI 克隆它们。 Gitweb 访问仅用于可视化,不能用于克隆。

我也尝试使用Configure nginx to serve two websites,这似乎很有希望,因为这个问题与我的问题相似,但当我将location /git { 替换为:

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

    try_files $uri $uri/ =404;
}

我认为我之前提到的“组合方法”是正确的方法,或者是吗?我应该添加、删除或修改某些内容以获取about:blank#blocked 吗?还是我完全迷路了?

【问题讨论】:

    标签: nginx gitweb


    【解决方案1】:

    试试这个:

    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
    }
    

    【讨论】:

    • 没用。我仍然收到about:blank#blocked
    猜你喜欢
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-23
    • 2019-02-12
    相关资源
    最近更新 更多