Linux nginx 不侦听端口 80
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16021481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
nginx not listening to port 80
提问by Nyxynyx
I've just installed a Ubuntu 12.04 server and nginx 1.2.7, removed default
from sites-enabled and added my own file into sites-available
and symlink at sites-enabled
. Then restarted nginx.
我刚刚安装了Ubuntu的12.04服务器和nginx的1.2.7,除去default
往返加入我自己的文件,用于支持网站,sites-available
在和符号链接sites-enabled
。然后重启nginx。
Problem:However going to the URL does not load the site. netstat -nlp | grep nginx
and netstat -nlp | grep 80
both returns no results! lsof -i :80
also returns nothing. A dig
from another server returns the correct ip address so it shouldn't be a DNS problem. I was able to connect to apache which I have now stopped its service. nginx logs also show nothing.
问题:但是转到 URL 不会加载站点。netstat -nlp | grep nginx
并且netstat -nlp | grep 80
都没有返回结果!lsof -i :80
也不返回任何内容。dig
来自另一台服务器的A返回正确的 IP 地址,因此它不应该是 DNS 问题。我能够连接到 apache,我现在已经停止了它的服务。nginx 日志也没有显示任何内容。
How should I troubleshoot this problem?
我应该如何解决这个问题?
/etc/nginx/site-available/mysite.com
/etc/nginx/site-available/mysite.com
server {
listen 80;
server_name www.mysite.com mysite.com *.mysite.com;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
root /var/www/mysite/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args ;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_read_timeout 300;
}
}
回答by Andres
You are probably binding nginx to port 80 twice. Is that your full config file? Don't you have another statement listening to port 80?
您可能将 nginx 绑定到端口 80 两次。那是你的完整配置文件吗?不是有另外一条语句监听80端口吗?
回答by Dionysius
Have you checked if your nginx binary really exists? please check if
您是否检查过您的 nginx 二进制文件是否真的存在?请检查是否
#whereis nginx
outputs the binary path and check this path with your init script from /etc/init.d/nginx. e.g.
输出二进制路径并使用 /etc/init.d/nginx 中的 init 脚本检查此路径。例如
DAEMON=/usr/sbin/nginx
(In my init script "test -x $DAEMON || exit 0" is invoked and in any case this script returned nothing - my binary was completely missing)
(在我的 init 脚本中调用了“test -x $DAEMON || exit 0”,无论如何这个脚本什么都不返回——我的二进制文件完全丢失了)
回答by Patrick
If your logs are silent on the issue, you may not be including the sites-enabled directory. One simple way to tell that the site is being loaded is to set the error/access log path within your server block to a unique path, reload nginx, and check if the files are created.
如果您的日志没有提及该问题,则您可能没有包含启用站点的目录。判断站点正在加载的一种简单方法是将服务器块中的错误/访问日志路径设置为唯一路径,重新加载 nginx,并检查文件是否已创建。
Ensure the following include directive exists within the http context in /etc/nginx/nginx.conf.
确保 /etc/nginx/nginx.conf 中的 http 上下文中存在以下包含指令。
http {
...
include /etc/nginx/sites-enabled/*;
}
回答by thcipriani
I had this same problem, the solution was that I had not symlinked my siteconf file correctly. Try running vim /etc/nginx/sites-enabled/mysite.com
—can you get to it? I was getting "Permission Denied."
我遇到了同样的问题,解决方案是我没有正确地符号链接我的 siteconf 文件。试着跑步vim /etc/nginx/sites-enabled/mysite.com
——你能做到吗?我收到“权限被拒绝”。
If not run:
如果没有运行:
rm /etc/nginx/sites-enabled/mysite.com
ln -s /etc/nginx/sites-available/mysite.com /etc/nginx/sites-enabled/mysite.com
回答by dayuloli
I ran into the same problem, I got a Failed to load resource: net::ERR_CONNECTION_REFUSED
error when connecting over HTTP, but fine over HTTPS. Ran netstat -tulpn
and saw nginx not binding to port 80 for IPv4. Done everything described here. Turned out to be something verystupid:
我遇到了同样的问题,Failed to load resource: net::ERR_CONNECTION_REFUSED
通过 HTTP 连接时出错,但通过 HTTPS 连接没问题。跑netstat -tulpn
并看到 nginx 没有绑定到 IPv4 的端口 80。完成这里描述的一切。原来是一件非常愚蠢的事情:
Make sure the sites-available
file with the default_server
is actually enabled.
确保实际启用了sites-available
带有的文件default_server
。
Hope this saved some other poor idiot out there some time.
希望这能拯救其他一些可怜的白痴。
回答by noraj
A semi-colon ;
missing in /etc/nginx/nginx.conf
for exemple on the line before include /etc/nginx/servers-enabled/*;
can just bypass this intruction and nginx -t
check will be successful anyway.
例如,在行之前;
缺少分号可以绕过此指令,无论如何检查都会成功。/etc/nginx/nginx.conf
include /etc/nginx/servers-enabled/*;
nginx -t
So just check that all instructions in /etc/nginx/nginx.conf
are ended with a semi-colon ;
.
所以只需检查所有指令/etc/nginx/nginx.conf
是否以分号结尾;
。
回答by spencer.sm
I've found it helpful to approach debugging nginx with the following steps:
我发现通过以下步骤调试 nginx 很有帮助:
1... Make sure nginx is running.
1...确保nginx正在运行。
ps aux | grep nginx
2... Check for processes already bound to the port in question.
2... 检查已绑定到相关端口的进程。
lsof -n -i:80
3... Make sure nginx has been reloaded.
3...确保已重新加载nginx。
sudo nginx -t
sudo nginx -s reload
On Mac,
brew services restart nginx
is notsufficient to reload nginx.
在Mac上,
brew services restart nginx
是不足够的重装nginx的。
4... Try creating simple responses manually to make sure your location path isn't messed up. This is especially helpful when problems arise while using proxy_pass
to forward requests to other running apps.
4... 尝试手动创建简单的响应,以确保您的位置路径不会被弄乱。当使用proxy_pass
将请求转发到其他正在运行的应用程序时出现问题时,这尤其有用。
location / {
add_header Content-Type text/html;
return 200 'Here I am!';
}
回答by Silveri
In my case those network command's outputs showed nginx was correctly binding to port 80, yet the ports weren't externally accessible or visible with nmap
.
就我而言,这些网络命令的输出显示 nginx 正确绑定到端口 80,但端口无法从外部访问或使用nmap
.
While I suspected a firewall, it turns out that old iptables
rules on the machine were redirecting traffic from those ports and conflicting with nginx. Use sudo iptables-save
to view all currently applicable rules.
虽然我怀疑有防火墙,但事实证明iptables
机器上的旧规则正在重定向来自这些端口的流量并与 nginx 冲突。使用sudo iptables-save
查看当前所有适用的规则。
回答by Chris
While we all think we don't make silly mistakes, we do.
虽然我们都认为我们不会犯愚蠢的错误,但我们确实会犯。
So, if you are looking into NGINX issues and all signs are showing it should work then you should take a step away from the files and look downstream.
因此,如果您正在调查 NGINX 问题并且所有迹象都表明它应该可以工作,那么您应该远离文件并查看下游。
System Firewall, Hardware Firewall, Nat router/firewall.
系统防火墙、硬件防火墙、Nat 路由器/防火墙。
For myself this issue was my router, I run a home lab and so I can access services behind my router from afar I use NGINX to reverse proxy as my router only handles incoming based on IP and doesn't do any handling of hostnames, I'm sure this is all fairly normal.
就我自己而言,这个问题是我的路由器,我运行一个家庭实验室,所以我可以从远处访问我的路由器后面的服务我使用 NGINX 进行反向代理,因为我的路由器只处理基于 IP 的传入并且不处理任何主机名,我我确信这一切都很正常。
In any case my issue cropped up as I was securing my network a few days ago, removing some port forwarding that isnt needed any longer and I accidentally removed port 80.
无论如何,几天前我在保护我的网络时出现了我的问题,删除了一些不再需要的端口转发,我不小心删除了端口 80。
Yes it was as simple as forwarding that port again to NGINX and all was fixed.
是的,就像再次将该端口转发到 NGINX 一样简单,并且一切都已修复。
I will now walk away with my head hung in extreme shame though I leave this answer to show my gratitude to the people in this thread that lead me to find my own error.
我现在将带着极度羞耻的头走开,尽管我留下这个答案是为了表达我对这个帖子中的人的感激之情,这些人让我发现了自己的错误。
So thank you.
所以谢谢。
回答by kuldeep Mishra
I had faced the same problem over the server, here I am listing the how I had solved it :
我在服务器上遇到了同样的问题,在这里我列出了我是如何解决它的:
Step 1 :: Installing the Ngnix
步骤 1 :: 安装 Ngnix
sudo apt update
sudo apt install nginx
Step 2 – Adjusting the Firewall
步骤 2 – 调整防火墙
sudo ufw app list
You should get a listing of the application profiles:
您应该获得应用程序配置文件的列表:
Output Available applications: Nginx Full Nginx HTTP Nginx HTTPS OpenSSH
Output Available applications: Nginx Full Nginx HTTP Nginx HTTPS OpenSSH
As you can see, there are three profiles available for Nginx:
如您所见,Nginx 有三个可用的配置文件:
Nginx Full:This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
Nginx Full:此配置文件同时打开端口 80(正常、未加密的网络流量)和端口 443(TLS/SSL 加密流量)
Nginx HTTP:This profile opens only port 80 (normal, unencrypted web traffic)
Nginx HTTP:此配置文件仅打开端口 80(正常、未加密的网络流量)
Nginx HTTPS:This profile opens only port 443 (TLS/SSL encrypted traffic)
Nginx HTTPS:此配置文件仅打开端口 443(TLS/SSL 加密流量)
Since I haven't configured SSL for our server yet in this guide, we will only need to allow traffic on port 80.You can enable this by typing:
由于我尚未在本指南中为我们的服务器配置 SSL,因此我们只需要允许端口 80 上的流量。您可以通过键入以下内容来启用它:
sudo ufw allow 'Nginx HTTP'
You can verify the change by typing:
您可以通过键入以下内容来验证更改:
sudo ufw status
Step 3 – Checking your Web Server
第 3 步 – 检查您的 Web 服务器
systemctl status nginx
Now Check port 80 , It worked for me hope will work for you as well.
现在检查端口 80 ,它对我有用,希望对你也有用。