![Nginx 没有响应使用 Capistrano 在 AWS EC2 实例上部署的 Rails 应用程序](https://linux22.com/image/662841/Nginx%20%E6%B2%A1%E6%9C%89%E5%93%8D%E5%BA%94%E4%BD%BF%E7%94%A8%20Capistrano%20%E5%9C%A8%20AWS%20EC2%20%E5%AE%9E%E4%BE%8B%E4%B8%8A%E9%83%A8%E7%BD%B2%E7%9A%84%20Rails%20%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F.png)
我在 aws ec2 实例上通过 capistrano 部署了一个 rails 应用程序,服务器上还安装了 Passenger nginx。每当我尝试使用 DNS 或提供的公共 IP 访问我的应用程序时,我得到的只是浏览器加载一段时间,最后没有响应,甚至没有来自 nginx 的错误页面。这是我的 nginx 配置 (/etc/nginx/sites-enabled/default)
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name <public ip>;
passenger_enabled on;
rails_env production;
root /home/ubuntu/myapp/current/public;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
运行命令“sudo nginx”后,我得到的响应如下:
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
我花了几个小时寻找解决方案,但一直无法访问我的应用程序。任何帮助甚至正确方向的指导都将不胜感激。如果需要更多信息,请告诉我。
编辑
在 error.log 中我得到的内容如下:
[ 2015-07-21 12:59:54.2681 21794/7eff039a4700 age/Cor/Main.cpp:507 ]: Signal received. Gracefully shutting down... (send signal 2 more time(s) t$
[ 2015-07-21 12:59:54.2681 21794/7eff0a1747c0 age/Cor/Main.cpp:876 ]: Received command to shutdown gracefully. Waiting until all clients have di$
[ 2015-07-21 12:59:54.2682 21794/7eff031a3700 Ser/Server.h:752 ]: [ApiServer] Freed 0 spare client objects
[ 2015-07-21 12:59:54.2682 21794/7eff031a3700 Ser/Server.h:443 ]: [ApiServer] Shutdown finished
[ 2015-07-21 12:59:54.2683 21794/7eff039a4700 Ser/Server.h:752 ]: [ServerThr.1] Freed 128 spare client objects
[ 2015-07-21 12:59:54.2683 21794/7eff039a4700 Ser/Server.h:443 ]: [ServerThr.1] Shutdown finished
[ 2015-07-21 12:59:54.2688 21794/7eff0a1747c0 age/Cor/Main.cpp:942 ]: Passenger core shutdown finished
[ 2015-07-21 12:59:54.2695 21799/7fb5deb62700 age/Ust/Main.cpp:337 ]: Signal received. Gracefully shutting down... (send signal 2 more time(s) t$
[ 2015-07-21 12:59:54.2695 21799/7fb5e536d7c0 age/Ust/Main.cpp:399 ]: Received command to shutdown gracefully. Waiting until all clients have di$
[ 2015-07-21 12:59:54.2695 21799/7fb5deb62700 Ser/Server.h:752 ]: [LoggerApiServer] Freed 0 spare client objects
[ 2015-07-21 12:59:54.2696 21799/7fb5deb62700 Ser/Server.h:443 ]: [LoggerApiServer] Shutdown finished
[ 2015-07-21 12:59:54.2697 21799/7fb5e536d7c0 age/Ust/Main.cpp:424 ]: Passenger UstRouter shutdown finished
[ 2015-07-21 12:59:55.3126 21887/7f0fcdb1e7c0 age/Wat/Main.cpp:1274 ]: Starting Passenger watchdog...
[ 2015-07-21 12:59:55.3190 21890/7f1083bfc7c0 age/Cor/Main.cpp:957 ]: Starting Passenger core...
[ 2015-07-21 12:59:55.3191 21890/7f1083bfc7c0 age/Cor/Main.cpp:234 ]: Passenger core running in multi-application mode.
[ 2015-07-21 12:59:55.3288 21890/7f1083bfc7c0 age/Cor/Main.cpp:707 ]: Passenger core online, PID 21890
[ 2015-07-21 12:59:55.3356 21895/7fc7fb8a07c0 age/Ust/Main.cpp:430 ]: Starting Passenger UstRouter...
[ 2015-07-21 12:59:55.3445 21895/7fc7fb8a07c0 age/Ust/Main.cpp:311 ]: Passenger UstRouter online, PID 21895
我甚至尝试释放 80 端口:使用
sudo fuser -k 80/tcp
结果是:
80/tcp: 22143 22146 22147 22148 22149
这是否意味着它破坏了与同一端口的 5 个连接,这可能是问题所在?如果是,那么有没有办法只维护与该端口的一个连接,即运行我的应用程序的进程的连接。
此后,我重新启动了 nginx,结果还是一样,输入 sudo nginx 后,我得到了与上面相同的响应
编辑2
经过大量谷歌搜索后,我找到了这个命令“sudo lsof -i:80”,这是它产生的输出,如果这有帮助,那么请提出您的解决方案,我已经有一段时间没有遇到这个问题了。
nginx 18275 root 6u IPv4 1213246 0t0 TCP *:http (LISTEN)
nginx 18276 www-data 6u IPv4 1213246 0t0 TCP *:http (LISTEN)
nginx 18277 www-data 6u IPv4 1213246 0t0 TCP *:http (LISTEN)
nginx 18278 www-data 6u IPv4 1213246 0t0 TCP *:http (LISTEN)
nginx 18279 www-data 6u IPv4 1213246 0t0 TCP *:http (LISTEN)
我认为端口 80 上运行着多个连接,最终导致其无法响应,我可能错了,但请提出您的解决方案。
我甚至从 /etc/nginx/sites-enabled/default 中删除了默认文件,创建了一个新的配置文件并对其进行了符号链接,但问题仍然存在。我还检查了我的 ec2 实例安全组,它允许来自任何地方的端口 80 上的入站流量。是否还有其他问题,或者我寻找解决方案的方向是否错误?
运行sudo netstat -tulpn | grep :80
后输出如下
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 18878/nginx
另外要指出的是,我已经部署了我的项目 repo 的一个分支,而不是主分支本身,一旦它部署并运行,我计划将该分支合并到主分支并再次部署它。
编辑3
我已经从我的本地终端尝试过这个:
telnet <public ip> 80
这是我得到的回应
Trying <public ip>.
Connected <public ip>.
Escape character is '^]'.
答案1
您已经有一个绑定到端口 80 的进程。通过以下方式检查
netstat -ntp | find "80"
一旦获得其 PID(例如 1234),就发送信号以正常关闭该进程:
kill -15 1234
如果没有帮助并且进程仍在运行,请稍等片刻,然后向 PID 发送另一种信号类型:
kill -9 1234
此后,您可以再次启动 Nginx,不会出现任何问题。