使用nginx反代不同子域名到本机不同端口
配置DNS解析
DNS解析是配置域名和ip的关系,与端口无关
- 有备案域名
- 在阿里云域名解析中配置自己到二级域名
- 记录类型选择
A记录
,主机记录填写自己定义的二级域名
,记录值填写自己的主机地址
- 注意:DNS解析存在TTL时间,修改后可能不会立即生效
- 无备案域名
- 修改本机host文件
- Windows:
C:\windows\system32\drivers\etc
- Linux:
/etc/hosts
- MacOS:
/etc/hosts
- Windows:
- 将
自己定义的二级域名
都映射到127.0.0.1
- 刷新DNS
- Windows:
ipconfig /flushdns
- Linux:
systemctl restart nscd
- MacOS:
sudo killall -HUP mDNSResponder
- Windows:
- 修改本机host文件
配置nginx
- 安装nginx和配置自启动
# 安装nginx
sudo yum install nginx
# 配置开机自启
sudo systemctl enable nginx
# 启动nginx
sudo systemctl start nginx
# 查看nginx运行状态
sudo systemctl status nginx
# 可选,防火墙放行http和https(如果是云主机到话,请自行在安全组中开启)
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
# 启动nginx
start nginx
# 重新加载nginx配置文件,修改配置文件后必须执行才能生效
nginx -s reload
# 停止nginx,快速关闭不管有没有正在处理的请求
nginx -s stop
# 停止nginx,退出前完成已经接受的连接请求
nginx -s quit
# 重启nginx
nginx -s reopen
# 检查配置文件是否有错
nginx -t
配置反代
nginx到主配置文件默认在/etc/nginx/nginx.conf
下,通过include
引入了俩个位置到配置文件位置,俩个include
分别在http
标签和server
标签下# http下 include /etc/nginx/mime.types; # server下 include /etc/nginx/default.d/*.conf;
在
/etc/nginx/default.d/
下新增以.conf
后缀的文件,内容如下server { listen 80; server_name abc.test.com; location / { proxy_pass http://127.0.0.1:xxxx; # 转发规则 proxy_set_header Host $proxy_host; # 修改转发请求头,让xxxx端口的应用可以受到真实的请求 proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } server { listen 80; server_name def.test.com; location / { proxy_pass http://127.0.0.1:yyyy; # 转发规则 proxy_set_header Host $proxy_host; # 修改转发请求头,让yyyy端口的应用可以受到真实的请求 proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
配置gitlab使用外部nginx
修改GitLab的
github.rb
配置文件
1295 # add by fc
1296 web_server['external_users'] = ['nginx','root']
1297 web_server['username'] = 'nginx'
1298 web_server['group'] = 'nginx'
1299
1300 # web_server['external_users'] = []
1301 # web_server['username'] = 'gitlab-www'
1302 # web_server['group'] = 'gitlab-www'
1303 # web_server['uid'] = nil
1304 # web_server['gid'] = nil
1305 # web_server['shell'] = '/bin/false'
1306 # web_server['home'] = '/var/opt/gitlab/nginx'
1313 # add by fc
1314 nginx['enable'] = false
1315
1316 # nginx['enable'] = true
1317 # nginx['client_max_body_size'] = '250m'
1318 # nginx['redirect_http_to_https'] = false
1319 # nginx['redirect_http_to_https_port'] = 80
配置GitLab的Nginx配置文件
下载nginx的gitlab配置模板文件。下载地址
将
gitlab-omnibus-nginx.conf
移动到/etc/nginx/conf.d
目录修改YOUR_SERVER_FQDN为自己的域名
34 server_name YOUR_SERVER_FQDN; ## Replace this with something like gitlab.example.com
禁用
/etc/nginx/nginx.conf
中的默认的80端口的server配置,# 注释掉38~57行 sed -i "38,57s@^@# @g" /etc/nginx/nginx.conf` # 结果 38 # server { 39 # listen 80 default_server; 40 # listen [::]:80 default_server; 41 # server_name _; 42 # root /usr/share/nginx/html; 43 # 44 # # Load configuration files for the default server block. 45 # include /etc/nginx/default.d/*.conf; 46 # 47 # location / { 48 # } 49 # 50 # error_page 404 /404.html; 51 # location = /404.html { 52 # } 53 # 54 # error_page 500 502 503 504 /50x.html; 55 # location = /50x.html { 56 # } 57 # }
加载配置
# 测试nginx文件是否正确,出现`test is successful`说明成功
nginx -t
# 刷新gitlab配置,出现`gitlab Reconfigured!`说明成功
gitlab-ctl reconfigure
- 启动服务
systemctl start gitlab-runsvdir
gitlab-ctl start
systemctl start nginx
netstat -tunlp|grep nginx