【问题标题】:Can't access nginx sites on wsl from windows browser无法从 Windows 浏览器访问 wsl 上的 nginx 站点
【发布时间】:2021-11-02 15:39:03
【问题描述】:

我安装 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 172.28.11.207 获取我的 wsl IP 并将 172.28.11.207 mysite.local 添加到 windows 的主机然后尝试从 windows 浏览器访问 mysite.local 显示什么

我也设置了127.0.0.1 mysite.local,但也没有机会

每次更改主机后我都使用ipconfig /flushdns

windows快速启动配置关闭 windows下休眠的配置关闭了

然后我尝试了https://docs.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 设置为使用带有 wsl --set-version Ubuntu-20.04 1 的版本 1 并再次运行 curl mysite.local 现在我得到 curl: (52) Empty reply from server

【问题讨论】:

  • wsl2 每次启动 dist 时都会获得一个新 IP。 localhost 始终有效,但不使用 127.0.0.1。您要么需要在每次启动它时检查它获得的 IP 并使用它更新 Windows 中的主机文件,要么坚持使用localhost
  • 问题是它甚至没有运行一次
  • 本地主机转发在某些情况下可能会发生故障。在进一步进行故障排除之前,请退出 WSL 实例,从 PowerShell 或 CMD 运行 wsl --shutdown,然后重新启动 WSL 实例和服务。请参阅here 了解更多信息。
  • @NotTheDr01ds 我按照你的建议做了,还有链接上推荐的东西,但没有进展

标签: php windows-subsystem-for-linux nginx-config


【解决方案1】:

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

【讨论】:

  • 欢迎回到 Stack Overflow!请将其表述为有条件的解释性答案,以避免给人提出澄清问题而不是回答的印象(应该使用评论而不是答案,比较meta.stackexchange.com/questions/214173/…)。例如像“如果你的问题是......那么解决方案是......因为......。”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-18
  • 1970-01-01
  • 2020-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多