无法从Windows浏览器访问WSL上的Nginx站点

5

我安装了wsl2 => 然后ubuntu => 然后nginx => php.fpm(我需要的版本) 然后将mysite.local.conf添加到我的nginx配置文件中,但无法从Windows访问wsl上的站点

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 172.28.11.207,并在Windows的hosts文件中添加了172.28.11.207 mysite.local,然后尝试从Windows浏览器访问mysite.local,但什么也没有显示。

我还设置了127.0.0.1 mysite.local,但也没有成功。

每次更改hosts文件后,我都使用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


1
每次启动分发版,wsl2 都会获得一个新的 IP。localhost 始终可用,但不能使用 127.0.0.1。您需要检查它每次启动后获取的 IP,并在 Windows 的 hosts 文件中更新它,或者只是使用 localhost - M. Eriksson
问题在于它甚至没有运行一次。 - Arash Rabiee
1
本地主机转发在某些情况下可能会出现故障。在进一步进行故障排除之前,请退出您的WSL实例,从PowerShell或CMD运行wsl --shutdown,然后重新启动WSL实例和服务。有关更多信息,请参见此处 - NotTheDr01ds
@NotTheDr01ds,我按照你建议的做了,并且还尝试了链接中推荐的方法,但是没有进展。 - Arash Rabiee
1个回答

2

端口可能已经被另一个服务使用,比如IIS。我遇到了nginx服务启动问题,因为该端口已经被IIS占用。在关闭IIS服务后,nginx服务成功运行。


2
欢迎回到 Stack Overflow!请以解释性条件答案的形式表述,以避免给人询问澄清问题而不是回答的印象(应该使用评论而不是答案,请参见 https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead)。例如像“如果您的问题是...,那么解决方案是...,因为...。” - hakre

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