如何配置apache服务器以代理到http:// localhost:3000 /并排除某些URL?

时间:2019-05-07 04:31:29

标签: php regex apache proxy ubuntu-18.04

我希望Laravel使用客户端Nuxt JS,并使用Laravel作为后端管理面板和api。

这是我的代码,用于将Laravel项目代理到Nuxt JS,但无法正常工作。

<VirtualHost *:80>
    ServerName nuxt.local

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/nuxt/public

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    ProxyPassMatch /^(admin-panel)(.*)$ !

    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/
    ProxyPreserveHost on
    LogLevel debug
 </VirtualHost>

我想从代理中排除“ admin-panel”和“ api”路由。

3 个答案:

答案 0 :(得分:0)

  • 您可能想在您的apache目录中找到httpd.conf。然后,制作原件并保存。
  • 然后,搜索VirtualHost,您可能会发现类似于以下内容的内容:

<VirtualHost *:3000>
   ServerAdmin localhost
    DocumentRoot "/var/www/html/nuxt/public"
    ServerName localhost

    ProxyRequests On
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:3000/

    <Directory "/var/www/html/nuxt/public">
        Options Indexes FollowSymLinks MultiViews
        MultiviewsMatch Any
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

根据需要进行更改。您可能需要确保这是正确的,然后再添加:

ProxyPassMatch /^(admin-panel)(.*)$ !

也许,您想要这样的东西:

ProxyPassMatch ^(admin-panel)(.*)$ http://localhost:3000/

httpd.conf中进行每次更改后,您可能需要重新启动apache并进行测试以查看其是否有效。

答案 1 :(得分:0)

除了使用ProxyPassMatch指令之外,还可以尝试从代理传递中排除每个URL:

ProxyPass / http://localhost:3000/
ProxyPass /admin-panel !
ProxyPass /api !

也许也可以使用正则表达式将它们组合成一行,例如:ProxyPass ^/(admin-panel|api)(.*)$ !

答案 2 :(得分:0)

我已经解决了问题

我正在发布我的apache2配置代码。可能对任何人都有帮助。

<VirtualHost *:80>

  ServerName blog.net

  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/html/blog/public

  <LocationMatch "/"> 
    allow from all 
    Satisfy any 
    ProxyPass http://localhost:3000/
    ProxyPassReverse http://localhost:3000/
  </LocationMatch>

  <LocationMatch "/admin-panel/*"> 
    allow from all 
    Satisfy any 
    ProxyPass http://localhost/blog/public
    ProxyPassReverse http://localhost/blog/public
  </LocationMatch>

  <LocationMatch "/admin/*"> 
    allow from all 
    Satisfy any 
    ProxyPass http://localhost/blog/public
    ProxyPassReverse http://localhost/blog/public
  </LocationMatch>

  <LocationMatch "/api/*"> 
    allow from all 
    Satisfy any 
    ProxyPass http://localhost/blog/public
    ProxyPassReverse http://localhost/blog/public
  </LocationMatch>

  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

,然后在laravel route / web.php文件中写入

\URL::forceRootUrl(env('APP_URL'));

使用项目URL而不是代理URL