最新版Gitlab 更改自带nginx 为自建nginx

本文介绍了如何将最新版 Gitlab 的内置 Nginx 更改为自建 Nginx 服务器的过程,包括禁用 Gitlab 的自带 Nginx、安装自建 Nginx 以及相关的配置步骤。

一. gitlab 安装

  参考:https://www.cnblogs.com/weifeng1463/p/7714492.html

二. gitlab 配置

   禁用自带 nginx

vim /etc/gitlab/gitlab.rb

加入

nginx['enable'] = false

 重启gitlab

sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart

 三. nginx 安装

  参考:https://blog.youkuaiyun.com/db3265613/article/details/84581930

 四. nginx 配置

如果服务器资源不充裕的情况下,我们不能让一个gitlab 独自霸占服务器,那么就需要给gitlab动刀子了。

当我试图在gitlab服务自带nginx上创建一个站点时,想当然的创建一个.conf文件再包含进去,
但是当我运行 gitlab-ctl reconfigure 发现现实并不是那么如意,nginx所有配置文件全都恢复默认了,
所以这条路不通了。

但是如果直接启动本机自带的nginx呢?不用试都会知道两个nginx 公用一个80会有冲突根本启动不了,
网上虽然有将gitlab 的gitlab改个名字,但是非常不靠谱,而且根本行不通。

那么只能把gitlab自带的nginx废弃掉,用自建的nginx扩展性才会更好。



更改Gitlab使用的nginx
禁用gitlab的nginx启动
vim /etc/gitlab/gitlab.rb

更改配置:

nginx['enable'] = false

2. 更改自建nginx.conf配置

因为gitlab里的nginx有自定义的参数配置,所以大部分都最好继续沿用
以下为我的nginx.conf配置,没有把gitlab的/opt/gitlab/embedded/conf/nginx.conf 完全拿来用,
因为发现有的项会导致自建nginx卡在Starting nginx位置

  user gitlab-www gitlab-www;

worker_processes auto;

error_log /home/wwwlogs/nginx_error.log crit;

pid /usr/local/nginx/logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events
{
use epoll;
worker_connections 51200;
multi_accept on;
}

http
{
log_format gitlab_access '$remote_addr - $remote_user [$time_local] "$request_method $filtered_request_uri $server_protocol" $status $body_bytes_sent "$filtered_http_referer" "$http_user_agent"';
log_format gitlab_mattermost_access '$remote_addr - $remote_user [$time_local] "$request_method $filtered_request_uri $server_protocol" $status $body_bytes_sent "$filtered_http_referer" "$http_user_agent"';
proxy_cache_path proxy_cache keys_zone=gitlab:10m max_size=1g levels=1:2;
proxy_cache gitlab;
#include mime.types;
default_type application/octet-stream;
include /opt/gitlab/embedded/conf/mime.types;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;

sendfile on;
tcp_nopush on;

keepalive_timeout 60;

tcp_nodelay on;

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";

#limit_conn_zone $binary_remote_addr zone=perip:10m;
##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.

server_tokens off;
access_log off;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
# Remove private_token from the request URI
# In: /foo?private_token=unfiltered&authenticity_token=unfiltered&rss_token=unfiltered&...
# Out: /foo?private_token=[FILTERED]&authenticity_token=unfiltered&rss_token=unfiltered&...
map $request_uri $temp_request_uri_1 {
default $request_uri;
~(?i)^(?<start>.*)(?<temp>[\?&]private[\-_]token)=[^&]*(?<rest>.*)$ "$start$temp=[FILTERED]$rest";
}

# Remove authenticity_token from the request URI
# In: /foo?private_token=[FILTERED]&authenticity_token=unfiltered&rss_token=unfiltered&...
# Out: /foo?private_token=[FILTERED]&authenticity_token=[FILTERED]&rss_token=unfiltered&...
map $temp_request_uri_1 $temp_request_uri_2 {
default $temp_request_uri_1;
~(?i)^(?<start>.*)(?<temp>[\?&]authenticity[\-_]token)=[^&]*(?<rest>.*)$ "$start$temp=[FILTERED]$rest";
}

# Remove rss_token from the request URI
# In: /foo?private_token=[FILTERED]&authenticity_token=[FILTERED]&rss_token=unfiltered&...
# Out: /foo?private_token=[FILTERED]&authenticity_token=[FILTERED]&rss_token=[FILTERED]&...
map $temp_request_uri_2 $filtered_request_uri {
default $temp_request_uri_2;
~(?i)^(?<start>.*)(?<temp>[\?&]rss[\-_]token)=[^&]*(?<rest>.*)$ "$start$temp=[FILTERED]$rest";
}

# A version of the referer without the query string
map $http_referer $filtered_http_referer {
default $http_referer;
~^(?<temp>.*)\? $temp;
}

server
{
listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;
server_name pm_yunsee.vuln.cn;
index index.html index.htm index.php;
root /home/wwwroot/default;

#error_page 404 /404.html;

# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

include enable-php.conf;

location /nginx_status
{
stub_status on;
access_log off;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*\.(js|css)?$
{
expires 12h;
}

location ~ /.well-known {
allow all;
}

location ~ /\.
{
deny all;
}

#access_log /home/wwwlogs/access.log;
}
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for "$upstream_addr" "$upstream_response_time" $request_time $content_length';
include vhost/*.conf;
include /var/opt/gitlab/nginx/conf/nginx-status.conf;
}
再直接把/var/opt/gitlab/nginx/conf/gitlab-http.conf,完整复制到自建nginx的conf/vhost目录,无需修改。

至此就已经完成了移植,新增站点就在vhost里添加配置文件了。

  

### GitLab 中升级内置 Nginx 版本的方法 在 GitLab CE 或 EE 安装包中,默认集成了一个特定版本的 Nginx,用于提供 Web 服务支持。如果需要升级内置的 Nginx 版本,可以通过重新编译或更新 GitLab 包来实现。以下是具体方法: #### 方法一:通过官方源更新 GitLabGitLab 的内置 Nginx 是随 GitLab 软件包一起发布的,因此最简单的方式是通过更新到最新版GitLab 来获取更高版本的 Nginx。 1. **确认当前使用的 GitLab 版本** 使用以下命令查看当前安装的 GitLab 版本: ```bash gitlab-rake gitlab:env:info ``` 2. **添加最新的 GitLab 存储库** 如果尚未配置最新的存储库,则可以运行以下命令以确保能够获得最新版本: ```bash curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash ``` 3. **执行软件包更新** 更新现有的 GitLab 包至最新版本: ```bash sudo apt-get update && sudo apt-get upgrade gitlab-ce ``` 对于 CentOS/RHEL 用户: ```bash sudo yum makecache fast && sudo yum upgrade gitlab-ce ``` 4. **验证新版本中的 Nginx** 升级完成后,可通过以下方式检查新的 Nginx 版本: ```bash gitlab-ctl status nginx ``` 这将显示当前正在运行的 Nginx 及其版本号[^1]。 --- #### 方法二:手动替换内置 Nginx 如果希望保留当前的 GitLab 版本而不进行全面升级,可以选择手动替换内置的 Nginx 文件。 1. **下载目标版本的 Nginx 源码** 前往 [Nginx 官方网站](https://nginx.org/en/download.html),下载所需的稳定版本源码并解压: ```bash wget http://nginx.org/download/nginx-X.XX.tar.gz tar zxvf nginx-X.XX.tar.gz cd nginx-X.XX ``` 2. **停止 GitLab 和内置 Nginx** 在操作之前,需暂停 GitLab 和内置 Nginx 的运行: ```bash gitlab-ctl stop nginx ``` 3. **编译并安装自定义 Nginx** 编译过程中可以根据需求调整参数(如启用模块),然后覆盖默认路径下的 Nginx 文件: ```bash ./configure --prefix=/opt/gitlab/embedded/nginx \ --with-http_ssl_module \ --with-stream ... make && sudo make install ``` 4. **重启 GitLab 并测试** 替换完毕后,恢复 GitLab 的正常运行状态: ```bash gitlab-ctl start nginx ``` 5. **验证新版本** 确认已成功应用新版本的 Nginx: ```bash /opt/gitlab/embedded/nginx/sbin/nginx -v ``` --- #### 注意事项 - 手动替换内置组件可能会影响后续的 GitLab 升级过程,建议仅在必要时采用此法。 - 若计划长期维护定制化设置,考虑禁用内置 Nginx 并改用外部独立部署的 Nginx 实例[^2]。 ```python print("完成内置 Nginx 升级流程") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值