Nginx:无法在端口80上监听...仅在OSX 10.11上端口8080有效。

10

OSX 10.11 我安装了最新的nginx 1.9.9(从源代码编译) 配置文件/usr/local/conf/nginx.conf语法正确

监听8080端口时,没有问题,我可以请求http://example.local:8080

server {
    listen       8080;
    server_name  example.local;
    root HTML;

然后运行:

 sudo lsof -i -P | grep -i "80"
 nginx     8254           root   10u  IPv4 0x643a3abbad7bf485      0t0     TCP *:8080 (LISTEN)
 nginx     8392           yves   10u  IPv4 0x643a3abbad7bf485      0t0    TCP *:8080 (LISTEN)

但是,当我将端口更改为80时,我无法访问http://example.local:80

server {
    listen       80;
    server_name  example.local;
    root html;

sudo nginx -s reload
nginx     8254           root   10u  IPv4 0x643a3abbad7bf485      0t0    TCP *:8080 (LISTEN)
nginx     8254           root   18u  IPv4 0x643a3abbadbb9485      0t0    TCP *:80 (LISTEN)
nginx     8430           yves   10u  IPv4 0x643a3abbad7bf485      0t0    TCP *:8080 (LISTEN)
nginx     8430           yves   18u  IPv4 0x643a3abbadbb9485      0t0    TCP *:80 (LISTEN)

Safari无法打开页面,但我仍然可以请求http://example.local:8080

非常奇怪.... Nginx正在监听80端口...我该怎么办?


那么错误日志显示了什么? - trojanfoe
/usr/local/var/log/nginx/error.log是空的,同样的, /usr/local/var/log/nginx/access.log也是空的。当我查看ps aux时,nginx: worker process是由nobody运行的...这是正确的吗? - user762579
是的... 我运行 sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist /System/Library/LaunchDaemons/org.apache.httpd.plist:找不到指定的服务。 - user762579
就我个人而言,我通过Homebrew安装的v1.8.1也遇到了完全相同的问题。 - dwkns
在Mac OSX上似乎是规则...安全第一!因此我坚持这个规则并在大于1000的端口上运行所有测试。 - user762579
显示剩余4条评论
3个回答

15

只有使用sudo权限,才能在低于1024的端口上运行服务。

随时随地运行:

$ sudo brew services restart nginx

从系统开始:

$ sudo cp /usr/local/opt/nginx/*.plist /Library/LaunchDaemons
$ sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

请注意,在配置文件中必须设置服务器侦听80端口,并确保没有其他进程正在80端口上运行。


0
在终端中运行top命令,使用F3搜索NGINX进程。 在找到的top列表中的运行NGINX进程中复制PID号码。 将PID号码粘贴到/usr/local/nginx/logs/nginx.pid文件中。 只需将PID号码粘贴到nginx.pid文件的第一行即可。 即使之前已经停止了它,也要先停止nginx sudo nginx -s stop,然后再启动它sudo nginx
Nginx无法停止其进程,因为它不知道PID号码,而正在运行的实例绑定到相对端口,阻止第二个实例绑定到同一端口。
这是在MacOS Ventura上进行的全新安装。这是因为我在运行时更改了NGINX配置文件中PID文件的位置,因此它无法停止自己。

0

检查防火墙或iptables。

端口80可能被阻止。尝试停止防火墙或iptables服务。

如果被阻止了,则添加条目以允许端口80


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接