Apache Web主机服务器上的Angular Universal应用程序的.htaccess中应该包含什么?

时间:2019-10-23 18:11:47

标签: angular apache angular-universal

这个问题与我问过here的问题有关,但这是一个更具体的问题,因此我将单独询问。

我有一个要在Apache Web服务器上部署的Angular Universal Web应用程序。我安装了pm2来启动应用程序并使它保持活动状态。它正在侦听端口4000。它在日志中没有显示任何错误,但是当我进入网站时却收到403。

我认为.htaccess文件中的内容不正确,或者需要在httpd.conf文件中进行配置。我对那些文件中到底需要什么感到困惑,因为我阅读的几个链接/文章显示了不同的配置。有人说您只需要.htaccess文件。有人说您还需要在httpd.conf文件中配置一个虚拟主机(我无法访问,因为我在服务器上没有root特权)。

这是文件夹结构的样子:

domains
    - appname.com
        - public_html
            - .htaccess
            - browser
                - index.html
                - other files...
            - server
            - server.js
            - package.json

这是我当前的.htaccess文件,该文件应适用于https并重定向到browser / index.html:

RewriteEngine On  

# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/browser/index.html

我还尝试了this configuration,该方法将虚拟主机放置在.htaccess文件中,但这不起作用。

总结我的问题: 要使Angular应用正常工作,.htaccess文件中到底需要什么? 虚拟主机标记是否也应该在.htaccess中使用?还是只能在httpd.conf文件中使用?

更新

我联系了我的虚拟主机。他们说不可能将虚拟主机添加到.htaccess文件中。因此,我需要的是重定向rewriteRule,其中考虑了https。

1 个答案:

答案 0 :(得分:0)

与我的虚拟主机一起,它可以正常工作。因此,它们放在httpd.conf文件中的是两个端口的代理设置(http的端口为80,https的端口为443)。我的应用程序在端口4000上运行,因此这就是他们不得不向该端口添加代理设置的原因。现在,httpd.conf文件可能看起来像这样:

<VirtualHost *:80>
 ServerName domainname.com
 ServerAlias www.domainname.com
 <Proxy *>
  Order allow,deny
  Allow from all
 </Proxy>
 ProxyPreserveHost On
 ProxyRequests Off
 ProxyPass / http://localhost:4000/
 ProxyPassReverse / http://localhost:4000/
</VirtualHost>

<VirtualHost *:443>
 ServerName domainname.com
 ServerAlias www.domainname.com
 <Proxy *>
  Order allow,deny
  Allow from all
 </Proxy>
 ProxyPreserveHost On
 ProxyRequests Off
 ProxyPass / https://localhost:4000/
 ProxyPassReverse / https://localhost:4000/
</VirtualHost>

因此,您应该将域名添加到serverName和serverAlias变量中,并将代理设置为localhost:4000或正在运行的任何角度。

然后,我将.htaccess文件移动到index.html所在的浏览器文件夹中,并将其更改为:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d

    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(.*) /index.html [NC,L]
</IfModule>