12

LNMP如何配置Gitlab的反向代理和HTTPS

 2 years ago
source link: https://jw1.dev/2020/05/09/a01.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

LNMP如何配置Gitlab的反向代理和HTTPS

This article might contains outdated or misleading contents due to it is tagged as "Deprecated" by its author.
👨‍💻 2020-05-09

使用Gitlab CE版已经有一年多了,期间一直在使用http://ip:port的方式来进行访问。很明显,缺点有二:

  • 没有使用https,安全性堪忧。
  • 没有使用域名,看起来并不专业...(

初期使用的时候并不是没有考虑过这两个问题,但是网上的教程大多都是直接nginx配置反向代理,且没有https,(且大部分都长得一样,(╬▔皿▔)凸),所以按照他们的教程来,没有一次是成功的... 后来也就放弃了配置域名和https...

直到今天,决定再闯一次虎山,终于掌握了要点...啊~

# 修改Gitlab配置文件

假设你已经拥有一台服务器,一个域名,且已经解析到你使用的服务器上,你的服务器已经装好gitlab,而且可以正常使用ip地址进行访问。

# 使用vim编辑gitlab配置文件
vim /etc/gitlab/gitlab.rb

如果你还不会使用vim可以看看我写的另一篇博客《来自一个不喜欢用Vim的弱鸡程序员的Vim教程》。

# 修改gitlab外部访问链接,也就是你想配置的域名
# 这里假设你的外部域名为
external_url 'https://git.aaa.com'

这里是修改gitlab监听的外部nginx端口,端口号随意,只要别占用一些重要端口就可以。

# 使用`/`命令搜索 `nginx['listen_port']`
nginx['listen_port'] = 8118

然后:wq保存并退出

重新配置gitlab,然后重启gitlab服务

gitlab-ctl reconfigure
gitlab-ctl restart

# 使用lnmp配置https

我这里lnmp版本为1.5。

至于怎么用lnmp配置https,网上教程已经满天飞了,我就不多废话了...

不过还是提两句:

  • 最好的教程:官网 --> https://lnmp.org/install.html
  • 如果ssl配置的时候验证不成功,首先确认一下你的服务器80端口是否可以访问

# 编辑lnmp中的Nginx Vhost配置文件以进行反向代理

# 进入到这个文件夹,你应该就能看到所有的lnmp域名配置文件
cd /usr/local/nginx/conf/vhost
# 修改你的git域名配置
vim git.aaa.com.conf

你看到的格式应该如下:

server
    {
        listen 80;
        # listen [::]:80;
        # ...
    
    	# 可以在监听80端口这里添加一句,这样http就会自动跳转到https
    	return 301 https://$host$request_uri;
    
        location /
        {
            # ...
        }
    }
# 在文件头部添加本地git服务信息
upstream git {
    server localhost:8118;
}

server
    {
        listen 80;
... ... ... ... 

好了,80端口的部分不用管,我们主要看443部分,也就是https部分

server里上面部分不用动,下面的location先全部删除

server
    {
        listen 443 ssl http2;
        server_name git.jacky97.top ;
        ssl_certificate ;
        ssl_certificate_key ;
        ssl_session_timeout 5m;
        ssl_protocols ;
        ssl_prefer_server_ciphers ;
        ssl_ciphers ;
        ssl_session_cache ;
        ssl_dhparam ;
        include rewrite/other.conf;
        include enable-php.conf;
    
    	# ====== 重点 ======
    	# 这里就照抄网上的教程了
    	location / {
            client_max_body_size 512m;
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        
            # 不一样的是这里设置一下缓存,不然会出现400错误
        	proxy_buffer_size 64k;
            proxy_buffers 32 32k;
            proxy_busy_buffers_size 128k;
			
        	# 这里设置成你服务器gitlab的访问路径和刚刚设置的端口
            proxy_pass http://localhost:8118;
            index index.html index.htm;
        }
    }

# 如果出现http 502错误怎么办

# 进入/var/log文件夹
cd /var/log
# 将gitlab文件夹的权限设置为775
chmod -R 775 gitlab

# Web IDE打不开怎么办

一般是出现Mixed Context错误 —— https站引用了http资源。解决方法也很简单:

确认一下你的gitlab配置文件中外部链接是HTTPS开头,且gitlab自带的监听https是关闭状态。

external_url 'https://git.aaa.com'

nginx['listen_https'] = false

完成后,重新配置、重启gitlab和lnmp,再设置一遍log文件的权限

gitlab-ctl reconfigure
gitlab-ctl restart
lnmp restart
chmod -R 775 /var/log/gitlab

现在你就可以用https://git.aaa.com来访问你的gitlab了!如果有任何问题,欢迎下面的评论区域告诉我!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK