How can i configure apache server to Proxy to http

2019-08-02 21:04发布

问题:

I want my Laravel to use Client side Nuxt JS and use Laravel as backend admin-panel and api(s).

This is my code to proxy Laravel project to Nuxt JS but it's not properly working.

<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>

I want to exclude "admin-panel" and "api" routes from hitting the proxy.

回答1:

  • You might want to locate your httpd.conf inside your apache directory. Then, make an original copy and keep it.
  • Then, search for VirtualHost, you might find something similar to:

<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>

Make the changes, however you wish. You might want to make sure, this is correct before you add:

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

Maybe, you want something like this:

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

After each changes in httpd.conf, you might want to restart apache and test to see if it would work.



回答2:

Instead of using the ProxyPassMatch directive try just excluding each url from your proxy pass:

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

There is probably a way to combine them into one line with regular expression as well, something like: ProxyPass ^/(admin-panel|api)(.*)$ !



回答3:

I have solved the problem

I am posting my apache2 config code. Might be this can be helpful for any.

<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>

and in laravel routes/web.php file write the

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

to use project URL not proxy URL