Apache2 Rewrite + ProxyPass working together

好久不见. 提交于 2019-12-24 22:50:47

问题


I have apache web server running at port 80 and flask application at port 5000.

I need to process the following URLs and point to their ports accordingly.

http://hello_world.com/api/v1/en/user/register
http://hello_world.com/api/docs/
http://hello_world.com

What I need to do is

If the URL is http://hello_world.com, point it to my code_igniter router at index.php.

If the URL is http://hello_world.com/api/docs/, do not point to code_igniter, access straight to that folder.

If the URL is http://hello_world.com/api/v1/en/user/register, point it to my flask application at port 5000.

I have the following conf file. However, I kept getting 404 error when I tried to ProxyPass to port 5000.

File does not exist: /var/www/api/v1

However, my flask application router is supposed to route that /api/v1/... URI.

My conf looks like

<VirtualHost *:80>
    ServerAdmin admin@hello_world.com
    ErrorDocument 404 /index.php

# Rewrite for Code Igniter if the URI doesn't start with /api/
    RewriteCond %{REQUEST_URI} !^/api/(.+)$
    RewriteCond %{REQUEST_URI} !^(index\.php|robots\.txt|fonts|assets|uploads|images)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]

# ProxyPass if the URI is /api/v1/
    ProxyPreserveHost On
    ProxyPass ^/api/v1/(.+)$ http://localhost:5000/api/v1/$1
    ProxyPassReverse ^/api/v1/(.+)$ http://localhost:5000/api/v1/$1

    DocumentRoot /var/www/
    <Directory />
            Options FollowSymLinks
            AllowOverride All
    </Directory>
    <Directory /var/www/>
            Options FollowSymLinks MultiViews
            AllowOverride All
    </Directory>

...

</VirtualHost>

回答1:


The proxy definitions are semantically wrong. Try that definition instead:

ProxyRequests Off 
ProxyPreserveHost On 
ProxyPass "/api/v1/" "http://localhost:5000/api/v1/" 
ProxyPassReverse "/api/v1/" "http://localhost:5000/api/v1/"


来源:https://stackoverflow.com/questions/34562942/apache2-rewrite-proxypass-working-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!