Nginx服务重启失败:Job for nginx.service failed because

在部署一个网站的时候,可能需要更改Nginx的配置,然后重启Nginx服务,部署的网站才能够访问。
在这里先简单总结一下nginx部署网站的步骤吧。

nginx建站流程简介

  1. 安装Nginx。以下说明步骤中都使用相对路径,具体部署时将.替换为nginx安装所在路径即可。
  2. ./nginx/sites-available/下面创建你的网站的配置文件homepage.com,内容为:
    server{
        listen  port1; #你的网站程序里使用的端口号
        server_name nginx所在服务器的ip;
        location / {
            # Pass the request to Gunicorn
            proxy_pass http://127.0.0.1:port2; #网站端口号
            proxy_read_timeout 6000;
            proxy_set_header Host $host: port1;
            proxy_set_header X-Real-IP $remote_addr; #你的网站程序里使用的端口号
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    
  3. 将homepage.com软连接到./nginx/sites-enabled/下面。注意要使用绝对路径(假设nginx安装在/etc目录):
    ln -s /etc/nginx/sites-available/homepage.com /etc/nginx/sites-enabled/homepage.com
  4. 开放防火墙相关端口(这里是上面的配置文件里的port2)
    iptables -I INPUT -p tcp --dport port2 -j ACCEPT
  5. 启动你的网站(视你使用的网站开发工具而定)
    nohup python -m my_server &
  6. 重启nginx服务
    systemctl restart nginx

至此,理论上你的网站已经搭建好了,可以访问了。但是你可能会遇到一些问题,
下面就是本文的重点:

可能遇到的问题:

  1. 重启nginx失败,你可能会得到下面一条错误信息:
    Job for nginx.service failed because the control process exited with error code. Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journal
    那么你可以按照提示去看看到底是什么错误。
    但根据我的经验,启动失败很大概率原因是配置文件有问题。
    重点来了,你还可以通过这条命令去校验你的Nginx配置是否成功:
    nginx -t
    如果成功,他会给出如下信息:

    # nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    

    如果你的配置文件有问题,也能从错误信息中看出来:

    a duplicate default server for [::]:80 in /etc/nginx/nginx.conf:41
    

    错误已经很明显了,有多个配置文件里面使用了80端口。所以你可以用下面这条命令去查找一下:

    grep -R default_server /etc/nginx
    # 啰嗦一句,-R的意思是递归查找。这条命令会从 /etc/nginx下递归查找所有包含 default_server 的文件
    

    找到该文件,写个别的端口或者删掉没用的文件,这个错误就可以解决了!

  2. 查看Nginx服务状态你可以发现:

    # systemctl status nginx.service
    	● nginx.service - The nginx HTTP and reverse proxy server
       Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
       Active: failed (Result: exit-code) since 2018-11-06 12:31:21 JST; 15s ago
    ...
    11月 06 12:31:20 zhangch-101 nginx[1863]: nginx: [emerg] bind() to 0.0.0.0:5001 failed (98: Address already in use)
    11月 06 12:31:21 zhangch-101 systemd[1]: nginx.service: control process exited, code=exited status=1
    11月 06 12:31:21 zhangch-101 nginx[1863]: nginx: [emerg] still could not bind()
    11月 06 12:31:21 zhangch-101 systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
    11月 06 12:31:21 zhangch-101 systemd[1]: Unit nginx.service entered failed state.
    11月 06 12:31:21 zhangch-101 systemd[1]: nginx.service failed.
    

    这个错误比较明显,已经有进程占用这个端口了。
    这时你就应该去看看哪个进程占用了这个端口:
    netstat -nap | grep 5001
    但是根据我的经验,应该是你的网站配置文件把你的网站的本地端口和nginx端口写反了。
    也就是说把上面的nginx建站流程中第2步的port1,port2换一下,重启服务,这个问题应该就解决了。

  3. 没有sites-available文件
    自己在/etc/nginx/下创建一下就行。
    但是别忘了在/etc/nginx/nginx.conf中找到http {},在里面加上一句:

    include /etc/nginx/sites-enabled/*;
    
  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
这个错误是在启动nginx服务时遇到的。根据引用和引用中的信息,错误消息显示为&quot;Job for nginx.service failed because the control process exited with error code.&quot;。这个错误通常是由于nginx服务的控制进程异常退出导致的。要了解更多详细信息,可以使用命令&quot;systemctl status nginx.service&quot;和&quot;journalctl -xe&quot;来查看。引用中提到,重新启动nginx服务后,错误消失并且查看nginx服务状态显示为successful。因此,重新启动nginx服务可能是解决这个错误的方法。<span class=&quot;em&quot;>1</span><span class=&quot;em&quot;>2</span><span class=&quot;em&quot;>3</span> #### 引用[.reference_title] - *1* [启动nginx服务报错Job for nginx.service failed because the control process exited with error code.](https://blog.csdn.net/see__you__again/article/details/116123488)[target=&quot;_blank&quot; data-report-click={&quot;spm&quot;:&quot;1018.2226.3001.9630&quot;,&quot;extra&quot;:{&quot;utm_source&quot;:&quot;vip_chatgpt_common_search_pc_result&quot;,&quot;utm_medium&quot;:&quot;distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2&quot;}}] [.reference_item style=&quot;max-width: 50%&quot;] - *2* *3* [Nginx——Nginx启动报错Job for nginx.service failed because the control process exited with error ...](https://blog.csdn.net/li1325169021/article/details/118462921)[target=&quot;_blank&quot; data-report-click={&quot;spm&quot;:&quot;1018.2226.3001.9630&quot;,&quot;extra&quot;:{&quot;utm_source&quot;:&quot;vip_chatgpt_common_search_pc_result&quot;,&quot;utm_medium&quot;:&quot;distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2&quot;}}] [.reference_item style=&quot;max-width: 50%&quot;] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值