无法从 Windows 浏览器访问 wsl 上的 nginx 站点

Ara*_*iee 5 php windows-subsystem-for-linux nginx-config

我安装了 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;
        }

}

Run Code Online (Sandbox Code Playgroud)

然后跑了ln -s /etc/nginx/sites-available/mysite.local.conf /etc/nginx/sites-enabled/

然后使用 ifconfig 获取我的 wsl IP172.28.11.207并添加172.28.11.207 mysite.local到 Windows 主机然后尝试访问mysite.local从 Windows 浏览器进行访问,但没有显示任何内容

我也设置了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
Run Code Online (Sandbox Code Playgroud)

依然没有

当我尝试跑步时,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

小智 2

端口可能已被其他服务(例如 IIS)使用。我遇到了 nginx 服务启动问题,因为该端口已被 IIS 使用。关闭IIS服务后,nginx服务运行无任何故障。

  • 欢迎回到 Stack Overflow!请将其表述为解释的条件答案,以避免产生提出澄清问题而不是回答的印象(对此应使用评论而不是答案,请比较 https://meta.stackexchange.com/questions/214173 /为什么我需要 50 声誉来评论我可以做什么)。例如,“如果您的问题是……那么解决方案是……因为……”。 (2认同)