1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

wslのローカルホストサーバをwindows上のサーバと同じように公開したい!!

Last updated at Posted at 2023-06-26

wslのローカルホストサーバをwindowsのサーバと同じように公開したい!!

皆さん、wslで立てたサーバーを同じローカルネットワーク上で見たいとか思いますよね?
例えば、webサイトの出来栄えをスマホで確認したい時など。
しかし、最近は全てwslでプログラミングを行うようになっていて、ネットワーク上から見ることができません。
そんなときは、これを実行します。
wslのlocalhostのポート3000をwindowsのポート3000として公開します。
あくまで同じネットワーク限定の公開です。
connectportを変更すれば、また別のwslのポートにも使えます。
リバースプロキシに管理者権限が必要なので、もらえていなかった場合は管理者権限を要求するようになっています。
※別途、3000受信のファイアウォールを切る必要があります

openwslwifi.bat
@echo off
setlocal enabledelayedexpansion

net session >nul 2>&1
if %errorLevel% == 0 (
for /f "tokens=2 delims=:" %%A in ('ipconfig ^| findstr IPv4') do (
	if defined IP (
		set "WSLIP=%%A"
	) else (
		set "IP=%%A"
	)
)

netsh interface portproxy reset
netsh interface portproxy add v4tov4 listenaddress=!IP! listenport=3000 connectaddress=127.0.0.1 connectport=3000

echo wsl localhost port 3000 to windows !IP!:3000

pause
) else (
	powershell  -command start-process %0 -verb runas
)
endlocal

exit
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?