mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2024-11-15 21:21:00 +00:00
Bind to all interfaces (#5)
* Bind to all interfaces When running under WSL2, binding only to localhost via the 127.0.0.1 address only lets you access the server from within WSL2 itself. It does not allow you to access the server from Windows. With this change, you can access the web server from the WSL ip address. You can find your WSL ip address with ``` $ ip a | grep eth0 4: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 inet 192.168.161.12/20 brd 192.168.175.255 scope global eth0 ``` * Add comment about binding to localhost When running under WSL2, binding only to localhost via the 127.0.0.1 address only lets you access the server from within WSL2 itself. It does not allow you to access the server from Windows. Add a comment explaining how to work around this, by binding to all interfaces, instead of only to localhost.
This commit is contained in:
parent
24dccda00c
commit
f4ba849d91
1 changed files with 7 additions and 0 deletions
|
@ -11,6 +11,13 @@ async fn main() -> Result<(), anyhow::Error> {
|
|||
.await
|
||||
.map_err(anyhow::Error::from)
|
||||
.with_context(|| "Failed to connect to Postgres.")?;
|
||||
|
||||
// Here we choose to bind explicitly to localhost, 127.0.0.1, for security
|
||||
// reasons. This binding may cause issues in some environments. For example,
|
||||
// it causes connectivity issues running in WSL2, where you cannot reach the
|
||||
// server when it is bound to WSL2's localhost interface. As a workaround,
|
||||
// you can choose to bind to all interfaces, 0.0.0.0, instead, but be aware
|
||||
// of the security implications when you expose the server on all interfaces.
|
||||
let address = format!("127.0.0.1:{}", configuration.application_port);
|
||||
let listener = TcpListener::bind(address)?;
|
||||
run(listener, connection_pool)?.await?;
|
||||
|
|
Loading…
Reference in a new issue