当我在电脑上访问 localhost 时,我可以连接,但当我在主机电脑上访问路由器的公共 IP 时,页面会超时。它在我的手机上可以正常工作,我可以看到网站。
这是我的 nginx 配置:(我已将监听地址替换为 ***):
server {
listen 80;
server_name ***;
index index.html index.php;
access_log /var/log/nginx/localhost.access_log main;
error_log /var/log/nginx/localhost.error_log info;
root /var/www/localhost/htdocs;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/***/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/***/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
if ($scheme != "https") {
return 301 https://$host$request_uri;
} # managed by Certbot
#return 404; # managed by Certbot
}
答案1
缺少一些信息来确定什么可以解决您的问题。
我猜你需要设置“默认服务器”来从 localhost 获取某些内容。servername 选项告诉 nginx 仅监听发往配置的 IP 名称的请求。通常 localhost 不会被 servername 访问。
listen 80 default_server;
来自 nginx 手册(http://nginx.org/en/docs/http/request_processing.html):
在此配置中,nginx 仅测试请求的标头字段“Host”,以确定应将请求路由到哪个服务器。 如果其值与任何服务器名称都不匹配,或者请求根本不包含此标头字段,则 nginx 会将请求路由到此端口的默认服务器。