如何在WSL2中使用反向代理Apache网络在Docker中配置Browsersync

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

我试图让Browsersync,WordPress和Apache在WSL 2(Ubuntu)的Docker中工作。几年前,我在Windows上的XAMPP中使用了Browsersync,但我意识到这是一个更复杂的数量级。
除了Browsersync之外,一切都可以正常工作,我可以在Windows上通过端口80和443在mysite.localhost访问WordPress,但如果我在http://mysite.localhost:3000上使用Browsersync访问它,Apache日志不会响应,浏览器返回以下内容:

  1. This site cant be reached
  2. mysite.localhost unexpectedly closed the connection
  3. ERR_CONNECTION_CLOSED

如果我更改了一个文件,Browsersync会在日志中看到更改,但我无法让浏览器看到任何内容。我花了一个星期的时间,我发现最接近的是this question,作者自己回答说,虽然他们使用Nginx作为代理和Laravel,不知道有多大的区别。
以下是我的Docker Compose的相关部分:

  1. services:
  2. node:
  3. build:
  4. context: .
  5. dockerfile: ./docker/node/Dockerfile
  6. image: node:18.16.1-build
  7. container_name: node
  8. restart: always
  9. depends_on:
  10. - wordpress
  11. # user: "node"
  12. expose:
  13. - 3000
  14. - 3001
  15. volumes:
  16. - ./wordpress/source/themes/omikuji/src:/home/node/omikuji/src
  17. - ./wordpress/source/themes/omikuji/dist:/home/node/omikuji/dist
  18. - ./wordpress/source/themes/omikuji/bs-config.js:/home/node/omikuji/bs-config.js
  19. command: "npm run dev"
  20. networks:
  21. - apache
  22. database:
  23. #...database stuff
  24. networks:
  25. - apache
  26. wordpress:
  27. build:
  28. context: .
  29. dockerfile: ./docker/wordpress/Dockerfile
  30. image: wordpress:6.2.2-php8.1-build
  31. container_name: wordpress
  32. restart: always
  33. hostname: mysite.localhost
  34. depends_on:
  35. database:
  36. condition: service_healthy
  37. expose:
  38. - 80
  39. volumes:
  40. - ./wordpress/source/themes/omikuji/dist:/var/www/html/wp-content/themes/omikuji
  41. environment:
  42. TZ: ${TZ} # Set local time
  43. WORDPRESS_DB_HOST: database
  44. WORDPRESS_DB_USER: ${MARIADB_USER}
  45. WORDPRESS_DB_PASSWORD: ${MARIADB_PASSWORD}
  46. WORDPRESS_DB_NAME: ${MARIADB_DATABASE}
  47. WORDPRESS_DEBUG: 1
  48. networks:
  49. - apache
  50. phpmyadmin:
  51. # ...phpmyadmin stuff
  52. networks:
  53. - apache
  54. apache:
  55. build:
  56. context: .
  57. dockerfile: ./docker/apache/Dockerfile
  58. image: httpd:2.4-build
  59. container_name: apache
  60. ports:
  61. - 80:80
  62. - 443:443
  63. - 3000:3000
  64. - 3001:3001
  65. environment:
  66. TZ: ${TZ} # Set local time
  67. networks:
  68. - apache
  69. volumes:
  70. db_data:
  71. external: true
  72. networks:
  73. apache:
  74. name: apache

browsersync配置:

  1. module.exports = {
  2. "logLevel": "debug",
  3. "logConnections": false,
  4. "logFileChanges": true,
  5. "logSnippet": true,
  6. "open": false,
  7. "files": ["./dist/style.css", "./dist/js/*.js", "./dist/*.php"],
  8. "proxy": "http://node:3000",
  9. "host": "node",
  10. "port": 3000,
  11. "reloadDebounce": 2000,
  12. "watchOptions": {
  13. "awaitWriteFinish": true
  14. }
  15. };

httpd-vhosts.conf中的Apache虚拟主机:

  1. <VirtualHost *:80>
  2. ServerName localhost
  3. RewriteEngine On
  4. RewriteCond %{HTTP:X-Forwarded-Proto} !https
  5. RewriteCond %{HTTPS} !=on
  6. RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  7. </VirtualHost>
  8. <VirtualHost *:80>
  9. ServerName mysite.localhost
  10. # SSL redirection is in .htaccess
  11. # Reverse proxy
  12. ProxyPreserveHost On
  13. ProxyPass / http://wordpress:80/
  14. ProxyPassReverse / http://wordpress:80/
  15. RequestHeader set X-Forwarded-Proto http
  16. </VirtualHost>
  17. <VirtualHost *:443>
  18. ServerName mysite.localhost
  19. # Reverse proxy
  20. ProxyPreserveHost On
  21. ProxyPass / http://wordpress:80/
  22. ProxyPassReverse / http://wordpress:80/
  23. RequestHeader set X-Forwarded-Proto https
  24. # Enable SSL
  25. SSLEngine on
  26. SSLCertificateFile /etc/ssl/certs/mysite.localhost.pem
  27. SSLCertificateKeyFile /etc/ssl/private/mysite.localhost-key.pem
  28. </VirtualHost>
  29. <VirtualHost *:3000>
  30. ServerName mysite.localhost
  31. # Reverse proxy
  32. ProxyPreserveHost On
  33. ProxyPass / http://node:3000/
  34. ProxyPassReverse / http://node:3000/
  35. RequestHeader set X-Forwarded-Proto http
  36. # Enable SSL
  37. SSLEngine on
  38. SSLCertificateFile /etc/ssl/certs/mysite.localhost.pem
  39. SSLCertificateKeyFile /etc/ssl/private/mysite.localhost-key.pem
  40. </VirtualHost>

在WordPress目录的根目录下重写.htaccess中的规则:

  1. RewriteEngine On
  2. # If the request is not https on the development server (reverse proxy)
  3. RewriteCond %{HTTP:X-Forwarded-Proto} !https
  4. # If the request is not https on the production server
  5. RewriteCond %{HTTPS} !=on
  6. # Redirect to SSL
  7. RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Apache Dockerfile:

  1. FROM httpd:2.4
  2. # Copy the configuration files
  3. WORKDIR /usr/local/apache2/conf
  4. COPY ./localhost/apache/httpd.conf httpd.conf
  5. # Copy the configuration extra files
  6. WORKDIR /usr/local/apache2/conf/extra
  7. COPY ./localhost/apache/httpd-vhosts.conf httpd-vhosts.conf
  8. COPY ./localhost/apache/httpd-ssl.conf httpd-ssl.conf
  9. COPY ./localhost/apache/proxy-html.conf proxy-html.conf
  10. # Copy the SSL certificates
  11. WORKDIR /etc/ssl/certs
  12. COPY ./localhost/certs/localhost.pem localhost.pem
  13. COPY ./localhost/certs/mysite.localhost.pem mysite.localhost.pem
  14. COPY ./localhost/certs/phpmyadmin.localhost.pem phpmyadmin.localhost.pem
  15. # Copy the SSL certificate private keys
  16. WORKDIR /etc/ssl/private
  17. COPY ./localhost/private/localhost-key.pem localhost-key.pem
  18. COPY ./localhost/private/mysite.localhost-key.pem mysite.localhost-key.pem
  19. COPY ./localhost/private/phpmyadmin.localhost-key.pem phpmyadmin.localhost-key.pem
  20. # Change to default directory
  21. WORKDIR /usr/local/apache2

Node Dockerfile:

  1. FROM node:18.16.1
  2. # Copy SSL certificates
  3. WORKDIR /etc/ssl/certs
  4. COPY ./localhost/certs/mysite.localhost.pem mysite.localhost.pem
  5. WORKDIR /etc/ssl/private
  6. COPY ./localhost/private/mysite.localhost-key.pem mysite.localhost-key.pem
  7. # Copy configuration files to root
  8. RUN mkdir /home/node/omikuji
  9. WORKDIR /home/node/omikuji
  10. COPY ./wordpress/source/themes/omikuji/.stylelintrc.json .stylelintrc.json
  11. COPY ./wordpress/source/themes/omikuji/package.json package.json
  12. COPY ./wordpress/source/themes/omikuji/package-lock.json package-lock.json
  13. # Install npm packages
  14. RUN npm install
  15. # Add environment variables after installing npm
  16. ENV NPM_CONFIG_LOGLEVEL info
  17. ENV NODE_ENV development

节点日志响应文件更改:

  1. * Executing task in folder Development: docker logs --tail 1000 -f 78ed8100eca903cd446d057b7da9f05fefac45f31d97d865e9b32a9264c15665
  2. npm info using [email protected]
  3. npm info using [email protected]
  4. > [email protected] dev
  5. > npm-run-all --parallel localhost dev:scss
  6. npm info using [email protected]
  7. npm info using [email protected]
  8. npm info using [email protected]
  9. npm info using [email protected]
  10. > [email protected] dev:scss
  11. > sass --watch --verbose --style=expanded ./src/scss/style.scss:./dist/style.css
  12. > [email protected] localhost
  13. > browser-sync start --config 'bs-config.js'
  14. [Browsersync] [debug] -> Starting Step: Finding an empty port
  15. [Browsersync] [debug] Found a free port: 3000
  16. [Browsersync] [debug] Setting Option: {cyan:port} - {magenta:3000
  17. [Browsersync] [debug] + Step Complete: Finding an empty port
  18. [Browsersync] [debug] -> Starting Step: Getting an extra port for Proxy
  19. [Browsersync] [debug] + Step Complete: Getting an extra port for Proxy
  20. [Browsersync] [debug] -> Starting Step: Checking online status
  21. [Browsersync] [debug] Resolved www.google.com, setting online: true
  22. [Browsersync] [debug] Setting Option: {cyan:online} - {magenta:true
  23. [Browsersync] [debug] + Step Complete: Checking online status
  24. [Browsersync] [debug] -> Starting Step: Resolve user plugins from options
  25. [Browsersync] [debug] + Step Complete: Resolve user plugins from options
  26. [Browsersync] [debug] -> Starting Step: Set Urls and other options that rely on port/online status
  27. [Browsersync] [debug] Setting multiple Options
  28. [Browsersync] [debug] + Step Complete: Set Urls and other options that rely on port/online status
  29. [Browsersync] [debug] -> Starting Step: Setting Internal Events
  30. [Browsersync] [debug] + Step Complete: Setting Internal Events
  31. [Browsersync] [debug] -> Starting Step: Setting file watchers
  32. [Browsersync] [debug] + Step Complete: Setting file watchers
  33. [Browsersync] [debug] -> Starting Step: Merging middlewares from core + plugins
  34. [Browsersync] [debug] Setting Option: {cyan:middleware} - {magenta:List []
  35. [Browsersync] [debug] + Step Complete: Merging middlewares from core + plugins
  36. [Browsersync] [debug] -> Starting Step: Starting the Server
  37. [Browsersync] [debug] Proxy running, proxing: {magenta:http://node:3000}
  38. [Browsersync] [debug] Running mode: PROXY
  39. [Browsersync] [debug] + Step Complete: Starting the Server
  40. [Browsersync] [debug] -> Starting Step: Starting the HTTPS Tunnel
  41. [Browsersync] [debug] + Step Complete: Starting the HTTPS Tunnel
  42. [Browsersync] [debug] -> Starting Step: Starting the web-socket server
  43. [Browsersync] [debug] Setting Option: {cyan:clientEvents} - {magenta:List [ "scroll", "scroll:element", "input:text", "input:toggles", "form:submit", "form:reset", "click" ]
  44. [Browsersync] [debug] + Step Complete: Starting the web-socket server
  45. [Browsersync] [debug] -> Starting Step: Starting the UI
  46. [Browsersync] [debug] Setting Option: {cyan:session} - {magenta:1690129836067
  47. [Browsersync] [UI] Starting Step: Setting default plugins
  48. [Browsersync] [UI] Step Complete: %s Setting default plugins
  49. [Browsersync] [UI] Starting Step: Finding a free port
  50. [Browsersync] [UI] Step Complete: %s Finding a free port
  51. [Browsersync] [UI] Starting Step: Setting options also relevant to UI from BS
  52. [Browsersync] [UI] Step Complete: %s Setting options also relevant to UI from BS
  53. [Browsersync] [UI] Starting Step: Setting available URLS for UI
  54. [Browsersync] [debug] Getting option via path: {magenta:[ 'urls' ]
  55. [Browsersync] [UI] Step Complete: %s Setting available URLS for UI
  56. [Browsersync] [UI] Starting Step: Starting the Control Panel Server
  57. [Browsersync] [UI] Using port 3001
  58. [Browsersync] [UI] Step Complete: %s Starting the Control Panel Server
  59. [Browsersync] [UI] Starting Step: Add element events
  60. [Browsersync] [UI] Step Complete: %s Add element events
  61. [Browsersync] [UI] Starting Step: Registering default plugins
  62. [Browsersync] [UI] Step Complete: %s Registering default plugins
  63. [Browsersync] [UI] Starting Step: Add options setting event
  64. [Browsersync] [UI] Step Complete: %s Add options setting event
  65. [Browsersync] [debug] + Step Complete: Starting the UI
  66. [Browsersync] [debug] -> Starting Step: Merge UI settings
  67. [Browsersync] [debug] Setting Option: {cyan:urls} - {magenta:Map { "local": "http://localhost:3000", "external": "http://node:3000", "ui": "http://localhost:3001", "ui-external": "http://localhost:3001" }
  68. [Browsersync] [debug] + Step Complete: Merge UI settings
  69. [Browsersync] [debug] -> Starting Step: Init user plugins
  70. [Browsersync] [debug] Setting Option: {cyan:userPlugins} - {magenta:
  71. [Browsersync] [debug] + Step Complete: Init user plugins
  72. [Browsersync] Proxying: http://node:3000
  73. [Browsersync] Access URLs:
  74. ----------------------------------
  75. Local: http://localhost:3000
  76. External: http://node:3000
  77. ----------------------------------
  78. UI: http://localhost:3001
  79. UI External: http://localhost:3001
  80. ----------------------------------
  81. [Browsersync] Watching files...
  82. Sass is watching for changes. Press Ctrl-C to stop.
  83. [2023-07-23 16:30] Compiled src/scss/style.scss to dist/style.css.
  84. [Browsersync] File event [change] : dist/style.css
k4aesqcs

k4aesqcs1#

Browsersync中的一些东西似乎与Node不兼容,因为我写了自己的Node服务器,它在第一次尝试时连接到同一个docker WordPress服务的同一个目标地址和端口。
我的服务器完全可以使用live reload和SSL(hat tip chatGPT),我不再需要Browsersync。如果其他人有兴趣学习如何做到这一点,这些是我使用的Node包。

  1. "devDependencies": {
  2. "chokidar": "^3.5.3",
  3. "express": "^4.18.2",
  4. "http": "^0.0.1-security",
  5. "http-proxy-middleware": "^2.0.6",
  6. "https": "^1.0.0",
  7. "socket.io": "^4.7.1",
  8. "socket.io-client": "^4.7.2"
  9. },

chokidar包用于监视文件的更改,socket.io包用于实时重新加载,其他包用于核心服务器,SSL,代理请求到Docker WordPress容器以及注入Socket.io所需的HTML。
在Apache虚拟主机中,代理地址指向Docker中的Node容器的名称和端口,其中我的Node容器名称为node,端口为3000
还请注意upgrade=websocket指令。这是支持WebSocket的更新方法,是您所需要的全部,比不推荐的方法更容易使用WebSocket重写和路径重定向。

  1. <VirtualHost *:80>
  2. ServerName mysite.localhost
  3. # SSL redirection is in .htaccess
  4. # Reverse proxy with websocket
  5. ProxyPreserveHost On
  6. ProxyPass / http://node:3000/ upgrade=websocket
  7. ProxyPassReverse / http://node:3000/ upgrade=websocket
  8. RequestHeader set X-Forwarded-Proto http
  9. ProxyTimeout 60
  10. </VirtualHost>
  11. <VirtualHost *:443>
  12. ServerName mysite.localhost
  13. # Reverse proxy with websocket
  14. ProxyPreserveHost On
  15. ProxyPass / http://node:3000/ upgrade=websocket
  16. ProxyPassReverse / http://node:3000/ upgrade=websocket
  17. RequestHeader set X-Forwarded-Proto https
  18. ProxyTimeout 60
  19. # Enable SSL
  20. SSLEngine on
  21. SSLCertificateFile /etc/ssl/certs/myproject.localhost.pem
  22. SSLCertificateKeyFile /etc/ssl/private/myproject.localhost-key.pem
  23. </VirtualHost>
展开查看全部

相关问题