nginx 监听端口问题

因需求在搭建web的时候需要将ngin监听端口改为其他(非80端口),

1. 修改配置文件

[root@localhost html]# vim /etc/nginx/conf.d/default.conf 

修改

    listen  8085;#监听8085
    server_name  localhost;

ok,启动nginx服务

[root@localhost /]# systemctl start nginx.service
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

发生错误无法发启动

运行systemctl status nginx.service

[root@localhost /]# systemctl status nginx.service
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Mon 2018-10-01 02:34:00 CST; 4s ago
     Docs: http://nginx.org/en/docs/
  Process: 51507 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=1/FAILURE)
  Process: 94488 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)
 Main PID: 40098 (code=exited, status=0/SUCCESS)

Oct 01 02:33:59 localhost.localdomain systemd[1]: Starting nginx - high performance web server...
Oct 01 02:33:59 localhost.localdomain nginx[94488]: nginx: [emerg] bind() to 0.0.0.0:8085 failed (13: Permission denied)
Oct 01 02:34:00 localhost.localdomain systemd[1]: nginx.service: control process exited, code=exited status=1
Oct 01 02:34:00 localhost.localdomain systemd[1]: Failed to start nginx - high performance web server.
Oct 01 02:34:00 localhost.localdomain systemd[1]: Unit nginx.service entered failed state.
Oct 01 02:34:00 localhost.localdomain systemd[1]: nginx.service failed.

发现nginx无打开端口权限,检查是否被占用

[root@localhost /]# semanage port -l | grep 8085
[root@localhost /]# 

无占用

开启8085端口

[root@localhost /]# semanage port -a -t http_port_t -p tcp 8085

开启nginx 服务

[root@localhost /]# systemctl start nginx.service
[root@localhost /]# 

OK,查看nginx是否运行

[root@localhost /]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:8085            0.0.0.0:*               LISTEN      97153/nginx: master 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1421/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1418/cupsd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1630/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1421/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1418/cupsd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      1630/master         
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           1020/avahi-daemon:  
udp        0      0 0.0.0.0:68              0.0.0.0:*                           4168/dhclient       
udp        0      0 0.0.0.0:32880           0.0.0.0:*                           1020/avahi-daemon:  

 

<think>好的,我现在需要帮助用户解决如何在GitLab中修改Nginx的默认监听端口。首先,我应该回忆一下之前学过的相关知识,以及用户提供的引用内容。 用户提到了三个引用,都是关于修改GitLab配置文件的。引用1和3提到了修改/etc/gitlab/gitlab.rb文件中的nginx['listen_port']参数,而引用2是关于gitlab-workhorse的配置,可能和Nginx代理有关。用户的主要需求是更改Nginx监听端口,所以重点应该在nginx['listen_port']的设置上。 首先,我需要确认GitLab的配置文件位置,通常是/etc/gitlab/gitlab.rb。用户需要编辑这个文件,找到nginx相关的配置部分,将listen_port从默认的80改为其他端口,比如82。然后运行gitlab-ctl reconfigure使配置生效。但是根据引用1,修改后即使重新运行reconfigure,配置也不会被覆盖,这是需要注意的。 接下来,可能需要检查Nginx的实际配置文件,如/var/opt/gitlab/nginx/conf/gitlab-http.conf,确认监听端口是否已经更改。如果用户遇到问题,可能需要手动修改这个文件,不过通常gitlab-ctl reconfigure会自动处理。 另外,用户提到的gitlab-workhorse配置可能需要同步调整,特别是在反向代理的情况下。比如,如果Nginx监听端口变了,可能需要确保gitlab-workhorse的代理设置和Nginx端口一致,如引用2中的例子是8021,但用户的问题可能不需要调整这部分,除非有特殊需求。 用户可能的疑问包括:修改后是否需要重启服务?是的,运行reconfigure会自动重启相关服务。另外,防火墙设置是否需要调整?可能需要开放新的端口,比如82,这取决于用户的环境。 最后,要提醒用户验证配置是否生效,可以通过访问http://服务器IP:新端口,或者查看Nginx监听状态。例如使用netstat -tulnp | grep nginx来确认端口是否正确。 总结步骤应该是:编辑gitlab.rb,修改端口,运行reconfigure,检查配置文件和端口监听情况,处理可能的防火墙或反向代理设置。</think>### 如何在 GitLab 中更改 Nginx 的默认监听端口? #### 步骤说明 1. **修改 GitLab 主配置文件** 打开 `/etc/gitlab/gitlab.rb`,找到 `nginx['listen_port']` 参数并修改为目标端口(例如 82): ```ruby nginx['listen_port'] = 82 # 默认值为80,改为新端口号[^1][^3] ``` **注意**:如果配置文件中没有该参数,需手动添加。 2. **应用配置变更** 运行以下命令使配置生效: ```bash sudo gitlab-ctl reconfigure ``` 此操作会重新生成 Nginx 的配置文件并重启相关服务。 3. **验证 Nginx 配置** 检查生成的 Nginx 配置文件 `/var/opt/gitlab/nginx/conf/gitlab-http.conf`,确认监听端口已更新: ```nginx listen *:82; # 默认值为listen *:80; ``` 4. **处理反向代理(如有需要)** 如果使用反向代理(如 GitLab Workhorse),需确保代理端口Nginx 配置一致。例如在 `/etc/gitlab/gitlab.rb` 中设置: ```ruby gitlab_workhorse['listen_network'] = "tcp" gitlab_workhorse['listen_addr'] = "127.0.0.1:8021" # 与代理端口一致[^2] ``` 5. **开放防火墙端口** 若服务器启用了防火墙(如 `ufw` 或 `firewalld`),需允许新端口(例如 82): ```bash sudo ufw allow 82/tcp ``` 6. **验证服务状态** 访问 `http://<服务器IP>:82`,确认 GitLab 页面正常加载。也可通过命令检查端口监听状态: ```bash sudo netstat -tuln | grep 82 ``` #### 注意事项 - **避免端口冲突**:确保新端口未被其他服务占用。 - **反向代理场景**:若 Nginx 仅作为反向代理,需额外配置 `proxy_pass` 指向实际服务端口[^3]。 - **备份配置**:修改前建议备份 `/etc/gitlab/gitlab.rb`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值