如何诊断从同一家庭网络访问运行在WSL上的Apache服务器的问题?

ca1c2owp  于 2023-10-23  发布在  Apache
关注(0)|答案(1)|浏览(152)

概述

我正尝试在家用计算机上运行Windows Subsystem for Linux(WSL)的Windows 11计算机上设置Apache Web服务器。在WSL shell中使用ip a为eth0返回的IP地址,比如172.26.xx.xx,我可以从同一台计算机访问我的简单网页。但是,我无法从同一网络中的另一台计算机访问该页面。

详情

我的ports.conf文件位于/etc/apache2,除了一些注解外,还包含以下内容:

  1. Listen 80
  2. <IfModule ssl_module>
  3. Listen 443
  4. </IfModule>
  5. <IfModule mod_gnutls.c>
  6. Listen 443
  7. </IfModule>

去掉注解后,我的apache2.conf文件(位于/etc/apache2)包含

  1. DefaultRuntimeDir ${APACHE_RUN_DIR}
  2. PidFile ${APACHE_PID_FILE}
  3. Timeout 300
  4. KeepAlive On
  5. MaxKeepAliveRequests 100
  6. KeepAliveTimeout 5
  7. User ${APACHE_RUN_USER}
  8. Group ${APACHE_RUN_GROUP}
  9. HostnameLookups Off
  10. ErrorLog ${APACHE_LOG_DIR}/error.log
  11. LogLevel warn
  12. IncludeOptional mods-enabled/*.load
  13. IncludeOptional mods-enabled/*.conf
  14. Include ports.conf
  15. <Directory />
  16. Options FollowSymLinks
  17. AllowOverride None
  18. Require all denied
  19. </Directory>
  20. <Directory /usr/share>
  21. AllowOverride None
  22. Require all granted
  23. </Directory>
  24. <Directory /var/www/>
  25. Options Indexes FollowSymLinks
  26. AllowOverride None
  27. Require all granted
  28. </Directory>
  29. AccessFileName .htaccess
  30. <FilesMatch "^\.ht">
  31. Require all denied
  32. </FilesMatch>
  33. LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
  34. LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
  35. LogFormat "%h %l %u %t \"%r\" %>s %O" common
  36. LogFormat "%{Referer}i -> %U" referer
  37. LogFormat "%{User-agent}i" agent
  38. IncludeOptional conf-enabled/*.conf
  39. IncludeOptional sites-enabled/*.conf

我的000-default.conf文件位于/etc/apache 2/sites-available,包含

  1. <VirtualHost *:80>
  2. ServerName www.example.com
  3. ServerAlias example.com
  4. ServerAdmin [email protected]
  5. DocumentRoot /var/www/html
  6. <Directory /var/www/html>
  7. AllowOverride All
  8. Options All
  9. Require all granted
  10. </Directory>
  11. ErrorLog ${APACHE_LOG_DIR}/error.log
  12. CustomLog ${APACHE_LOG_DIR}/access.log combined
  13. </VirtualHost>

值得一提的是,我确实有一个注册域名,我用它来代替上面的 * example.com *。(因为我在没有静态IP地址的家庭路由器后面,所以我使用ddclient与我的提供商设置动态DNS。这似乎是工作,只要相同的IP地址显示为我的网络由https://whatismyipaddress.com/和自动填充字段中的管理页面我的DNS注册。然而,除非我完全误解了事情,否则这是无关紧要的,因为我不能从另一台计算机访问我的页面,即使在同一个网络中。

我所尝试的

老实说,没什么。Apache服务器的配置在我看来非常简单,对于来自同一台计算机的连接,它会立即运行。我唯一能想到的就是

  • 端口转发问题 *

在WSL shell上运行sudo ufw status verbose将返回

  1. Status: active
  2. Logging: on (low)
  3. Default: deny (incoming), allow (outgoing), deny (routed)
  4. New profiles: skip
  5. To Action From
  6. -- ------ ----
  7. 22/tcp ALLOW IN Anywhere
  8. 80/tcp ALLOW IN Anywhere
  9. 443 ALLOW IN Anywhere
  10. 80,443/tcp (Apache Full) ALLOW IN Anywhere
  11. 22/tcp (v6) ALLOW IN Anywhere (v6)
  12. 80/tcp (v6) ALLOW IN Anywhere (v6)
  13. 443 (v6) ALLOW IN Anywhere (v6)
  14. 80,443/tcp (Apache Full (v6)) ALLOW IN Anywhere (v6)

我觉得没问题按照here的说明,我运行netsh interface portproxy将Windows计算机上的端口80和443Map到WSL上的端口80和443。脚本中的关键命令包括

  1. netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr"

  1. netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport

其中$remoteport匹配ip a返回的IP地址172.26.xx.xx,而$addr保留为0.0.0.0。
我还按照https://www.nextofwindows.com/allow-server-running-inside-wsl-to-be-accessible-outside-windows-10-host的说明在Windows Defender防火墙中创建了一个入站规则,但我相信这些规则在WSL 2中被上面的说明所取代。这两种可能的解决方案都在Access a web server which is running on WSL (Windows Subsystem for Linux) from the local network上讨论过,但问题中的任何一个或任何其他建议都不适合我。
我不知道这个端口转发是否真的有必要,或者如何诊断它是否做了它应该做的事情。在任何情况下,我都在Windows的特权Powershell会话中运行netstat -aobn | findstr :80,它返回

  1. TCP 0.0.0.0:80 0.0.0.0:0 LISTENING 5720
  2. TCP 10.0.0.229:56431 10.0.0.249:8009 ESTABLISHED 11868
  3. TCP 10.5.0.2:54444 192.229.211.108:80 CLOSE_WAIT 22772
  4. TCP [::1]:80 [::]:0 LISTENING 9280

还有Get-NetTCPConnection | where localport -eq 80,它返回了,为了紧凑而编辑,

  1. LocalAddress LocalPort RemoteAddress RemotePort State AppliedSetting OwningProcess
  2. ------------ --------- ------------- ---------- ----- -------------- -------------
  3. ::1 80 :: 0 Listen 9280
  4. 0.0.0.0 80 0.0.0.0 0 Listen 5720

有人看到什么有趣的东西了吗?

  • 调查日志 *

在/var/logs/apache 2/error. log中有许多组,

  1. [Sun Oct 15 18:37:05.433219 2023] [mpm_event:notice] [pid 19762:tid 139729704814464] AH00493: SIGUSR1 received. Doing graceful restart
  2. [Sun Oct 15 18:37:05.441983 2023] [mpm_event:notice] [pid 19762:tid 139729704814464] AH00489: Apache/2.4.52 (Ubuntu) configured -- resuming normal operations
  3. [Sun Oct 15 18:37:05.441995 2023] [core:notice] [pid 19762:tid 139729704814464] AH00094: Command line: '/usr/sbin/apache2'

除此之外什么都没有,但这些都与我在更改后按此顺序运行sudo a2dissite 000-default.confsudo systemctl reload apache2sudo a2ensite 000-default.confsudo systemctl reload apache2的多次匹配。
以下是其他配置参数,在不太可能的情况下,它很重要:

  • 运行11版本23 H2构建版本25967.1010的桌面。
  • WSL版本2.0.3.0
  • Ubuntu 22.04.03,内核版本5.15.123.1-microsoft-standard-WSL 2
  • ApacheApache/2.4.52(Ubuntu)

我的问题是我如何诊断问题,因为这对我来说是一次学习经历,但当然也欢迎直接的解决方案。

5jvtdoz2

5jvtdoz21#

我最初的问题可能与在WSL中运行服务器有关。证据是,在专用的Linux机器上运行服务器时,我没有遇到任何问题。我放弃了原来的方法,转而采用这种新方法。

相关问题