Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Apache as a reverse proxy #308

Closed
Androz2091 opened this issue Jan 14, 2021 · 4 comments
Closed

Use Apache as a reverse proxy #308

Androz2091 opened this issue Jan 14, 2021 · 4 comments
Labels

Comments

@Androz2091
Copy link

Androz2091 commented Jan 14, 2021

Hello 👋
First of all, thank you for maintaining this awesome project, it's very useful!

I'm trying to setup cors-anywhere with Apache and Cloudflare. The following configuration works, but it seems like it can't parse the URL and I'm redirected to the help page every time.

<VirtualHost *:443>

    ServerName cors-anywhere.androz2091.fr

    ProxyPass / http://localhost:3700/
    ProxyPassReverse / http://localhost:3700/

    LogLevel warn
    ErrorLog ${APACHE_LOG_DIR}/cors.androz2091.fr.log
    CustomLog ${APACHE_LOG_DIR}/cors.androz2091.fr.log combined

</VirtualHost>

I tried to remove https:// (to use https://cors-anywhere.androz2091.fr/discord.com) instead of (https://cors-anywhere.androz2091.fr/https://discord.com) and it worked (but I need https for some calls).

I read the other issues, and I understand what the problem is, Apache uses URL normalization and the https in the URL breaks everything but I don't know how to setup Apache properly, as there are only fragments of code in #201). Does someone know how I could get it to work?

Thank you

@Androz2091
Copy link
Author

I fixed my problem using:

VirtualHost *:443>

    ServerName cors-anywhere.androz2091.fr

    ProxyPass /https:// http://localhost:3700/https://
    ProxyPassReverse /https:// http://localhost:3700/https://


    LogLevel warn
    ErrorLog ${APACHE_LOG_DIR}/cors.androz2091.fr.log
    CustomLog ${APACHE_LOG_DIR}/cors.androz2091.fr.log combined

</VirtualHost>

I hope that this will help someone 👍

@Rob--W
Copy link
Owner

Rob--W commented Jan 14, 2021

Thanks for sharing. Your original issue was caused by Apache that normalizes URLs (e.g. // -> /). Your solution is creative and will work for most common cases, but do note that the URL normalization may still affect other kinds of requests. For example if you were to call
/http://example.com/?redir=http://other.example.com, then the actual request will be to
/http://example.com/?redir=http:/other.example.com (note the // was replaced with /). If you come across something like this, url-encoding could be used as a work-around, e.g.
/http://example.com/?redir=http%3A%2F%2Fother.example.com

The ideal solution is to disable URL normalization altogether, but I haven't checked how to do that in Apache.

For those who come across this bug and are looking for the solution for nginx, see https://github.com/Rob--W/cors-anywhere/issues/143#issuecomment-452468128

@rainner
Copy link

rainner commented Dec 23, 2022

For anyone landing here looking for a solution. I found one that worked for me here:

https://stackoverflow.com/questions/67047224/apache-proxy-server-removing-double-slashes

There is a nocanon option that can be used with ProxyPass that will preserve the URL slashes:

https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxypass

ProxyPass / http://127.0.0.1:8080/ nocanon

@Its-Just-Nans
Copy link

If this is useful for someone

Here is a config for nginx

  1. start your cors-anywhere at localhost:4200
  2. Add this config to the nginx
  merge_slashes off;
  location / {
      proxy_pass          http://localhost:4200/;
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants