如何在 apache 服务器上同时使用 NODE 和 react.js 运行 next.js

如何在 apache 服务器上同时使用 NODE 和 react.js 运行 next.js

我必须同时在同一个域上运行 2 个不同的应用程序。如果 url 包含 /presscenter,我想将所有请求转发到 localhost:3000,如果不包含,则默认应为我的 react.js 应用程序。我在这里为 apache 虚拟主机找到了这个:

RewriteEngine On

# Rewrite rule for /presscenter requests
RewriteCond %{REQUEST_URI} ^/presscenter [NC]
RewriteRule ^(.*)$ http://localhost:3000/$1 [P]

# Rewrite rule for all other requests
RewriteCond %{REQUEST_URI} !^/presscenter [NC]
RewriteRule ^(.*)$ /index.html [L]

但同时,如果我希望我的 react.js 应用程序正常工作,我还需要有这个 .htaccess:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://my.website.ge/$1 [R,L]

#php_value short_open_tag 1
RewriteEngine on
Options +FollowSymlinks
ErrorDocument 404 /404.php
RewriteCond %{QUERY_STRING} ^fbclid=(.*)$


RewriteRule  ^pcms/? - [L]

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]

这两条规则正在自我覆盖(我猜),并且网站没有得到正常服务。我应该做哪些改变?

答案1

我假设您已经在服务器配置中启用/配置了 mod_proxy(和相关模块)。

请尝试以下操作:

Options +FollowSymlinks -MultiViews

ErrorDocument 404 /404.php

DirectoryIndex index.html

RewriteEngine On

# HTTP to HTTPS redirect
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://my.website.ge%{REQUEST_URI} [R=301,L]

# Rewrite rule for /presscenter requests
RewriteRule ^presscenter http://localhost:3000%{REQUEST_URI} [P]

# No further processing if URL starts "/pcms" and "fbclid" URL param at start of qs
RewriteCond %{QUERY_STRING} ^fbclid=
RewriteRule ^pcms - [L]

# Front-controller for react.js app
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.html [L]

我简化了一些正则表达式,例如,^pcms与 相同^pcms/?- 目前尚不清楚这是否真的是您的意图。(尽管我怀疑您可能打算将其匹配pcmspcms/<something>,在这种情况下,正则表达式应该是^pcms($|/)。)

有点奇怪的是,您还有/404.php(PHP 脚本?),尽管这很少使用。这ErrorDocument 404仅与在查询字符串开头启动/pcms并具有fbclidURL 参数且不映射到物理文件或目录的请求相关。


# Rewrite rule for all other requests
RewriteCond %{REQUEST_URI} !^/presscenter [NC]
RewriteRule ^(.*)$ /index.html [L]

这不是必需的(但也是错误的),因为请求会index.html被后面的规则重写。如果您重写任何未启动的请求,/presscenter那么它实际上会导致重写循环,因为它会将所有内容(包括所有静态资产)重写为自身(index.html)。

相关内容