我安装了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
localhost
始终可用,但不能使用127.0.0.1
。您需要检查它每次启动后获取的 IP,并在 Windows 的 hosts 文件中更新它,或者只是使用localhost
。 - M. Erikssonwsl --shutdown
,然后重新启动WSL实例和服务。有关更多信息,请参见此处。 - NotTheDr01ds