提问人:Arash Rabiee 提问时间:5/13/2021 最后编辑:Arash Rabiee 更新时间:11/2/2021 访问量:5603
无法从 Windows 浏览器访问 wsl 上的 nginx 站点
Can't access nginx sites on wsl from windows browser
问:
我安装了 wsl2 =>然后是 ubuntu =>然后是 nginx => php.fpm(我需要的版本)
然后添加到我的 nginx 配置中,但无法从 Windows 访问 wsl 上的站点mysite.local.conf
nginx 配置代码:
server {
listen 80 default_server;
server_name mysite.local *.mysite.local;
root /mnt/d/projects/arash/original/Web/frontend/web;
access_log /mnt/d/projects/arash/original/logs/arash-access.log;
error_log /mnt/d/projects/arash/original/logs/arash-error.log;
location ~* ^/backend$ {
rewrite ^ http://backend.mysite.local permanent; #301 redirect
}
location ~* ^/setting$ {
rewrite ^ http://setting.mysite.local permanent; #301 redirect
}
if ($host ~* ^(arash\.local)) {
rewrite ^ http://www.$host$uri permanent; #301 redirect
}
location ~ /\. {
deny all;
}
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_read_timeout 1h;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* ^.+\.(rss|atom|jpg|jpeg|gif|png|ico|rtf|js|css|svg|woff)$ {
expires 7d;
}
charset utf8;
}
server {
listen 80;
server_name backend.mysite.local;
access_log /mnt/d/projects/arash/original/logs/arash-access.log;
error_log /mnt/d/projects/arash/original/logs/arash-error.log;
root /mnt/d/projects/arash/original/Web/backend;
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_read_timeout 1h;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 80;
server_name setting.mysite.local;
access_log /mnt/d/projects/arash/original/logs/arash-access.log;
error_log /mnt/d/projects/arash/original/logs/arash-error.log;
root /mnt/d/projects/arash/original/Web/setting/web;
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_read_timeout 1h;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
然后跑了ln -s /etc/nginx/sites-available/mysite.local.conf /etc/nginx/sites-enabled/
然后使用 ifconfig 获取了我的 wsl IP 并添加到 Windows 主机,然后尝试从 Windows 浏览器访问,什么也没显示172.28.11.207
172.28.11.207 mysite.local
mysite.local
我也设置了,但也没有机会127.0.0.1 mysite.local
每次更换主机后我都使用ipconfig /flushdns
Windows 上快速启动的配置已关闭 Windows 上的休眠配置已关闭
然后我尝试 https://learn.microsoft.com/en-us/windows/wsl/wsl-config#wsl-2-settings 这个并使用以下代码将 .wslconfig 添加到我的用户:
[wsl2]
localhostForwarding=true
还是没有
当我试图跑步时,我得到了curl mysite.local
curl: (7) Failed to connect to mysite.local port 80: Connection refused
然后我将 wsl 设置为使用版本 1 并再次运行,现在我得到wsl --set-version Ubuntu-20.04 1
curl mysite.local
curl: (52) Empty reply from server
答:
端口可能已被其他服务(如 IIS)使用。我遇到了 nginx 服务启动问题,因为该端口已被 IIS 使用。关闭 IIS 服务后,nginx 服务运行没有任何故障。
评论
localhost
127.0.0.1
localhost
wsl --shutdown