LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 04-16-2019, 07:32 AM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Rep: Reputation: 103Reputation: 103
letsencrypt exception on apache reverse proxy along with ip restrictions


I have the following apache configuration for a reverse proxy:
Code:
<VirtualHost *:80>
  ServerName example.com
  DocumentRoot /var/www/html
  Redirect / https://example.com/
</VirtualHost>

<VirtualHost *:443>
  ServerName example.com
  
  SSLEngine on
  SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
  DocumentRoot /var/www/html


  ProxyRequests Off
  ProxyPreserveHost On
<Location />
    ProxyPass "http://localhost:8050/"
    ProxyPassReverse "http://localhost:8050/"
    Require ip 10.0.0.0/24
    Require ip 192.168.0.0/24
</Location>
</VirtualHost>
Now if I want to create an exception for let's encrypt so that it doesn't proxy requests coming from their servers I would normally do it like that:
Code:
ProxyPass /.well-known !
ProxyPass / "http://localhost:8050/"
ProxyPassReverse / "http://localhost:8050/"
That is to say, outside the "Location" section (which means that I explicitly write the path of the ProxyPass and ProxyPassReverse, as it is no longer implicit). The problem is that I wouldn't be able to integrate the IP restrictions, which I would like to have only for the proxied backend and I would leave the /.well-known location accessible.

Any ideas how I can achieve this?

Last edited by vincix; 04-16-2019 at 07:34 AM.
 
Old 04-16-2019, 10:40 AM   #2
tyler2016
Member
 
Registered: Sep 2018
Distribution: Debian, CentOS, FreeBSD
Posts: 243

Rep: Reputation: Disabled
You could use mod_rewrite to send requests from your internal networks to the proxied backends unmodified, and everything else to the file Let's Encrypt uses to verify domain ownership. Unless you are running a super high traffic site where you need to squeeze every bit of performance out of it, it should do the job.
 
Old 04-16-2019, 03:03 PM   #3
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
I find the solution a little bit ambiguous. So what you're saying is that I should add rewrite rules for each of network address? There must be a clearer and cleaner way of doing this, but maybe I'm wrong. But if you're willing to be more explicit, then I'll give it a try.
 
Old 04-17-2019, 06:30 AM   #4
tyler2016
Member
 
Registered: Sep 2018
Distribution: Debian, CentOS, FreeBSD
Posts: 243

Rep: Reputation: Disabled
Maybe something like this?

Code:
RewriteEngine On
RewriteCond %{CONN_REMOTE_ADDR} (!10\.0\.0\..*)|(!192\.168\.0\..*)
RewriteRule #LETSENCRYPT_URI
 
Old 04-17-2019, 08:18 AM   #5
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
On the one hand, I have a long list of IPs that I need to give access to. On the other hand, I don't see how the rule would work anyway, as the letsencrypt_uri is going to be example.com/.well-known, which doesn't really help me in this context, as it's going to go to the backend anyway, so that the letsencrypt requests are going to end up accessing http://localhost:8050/.well-known instead.

I don't understand why it wouldn't work with an Alias and/or with another Location for /.well-known. I tried it, but it doesn't seem to have any effect. I added the following before <Location />

Code:
Alias "/.well-known" "/var/web/letsencrypt"
  <Directory "/var/web/letsencrypt">
  Require all granted
  </Directory>
I was able to do it in nginx so easily while also including ip restriction in another location:

Code:
location /.well-known {
                alias /var/www/html/.well-known;
    }
location / {
        include allowed.ips; # restrict access to certain IPs
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://mybackend;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        # This allows the ability for the execute shell window to remain open for up to 15 minutes. Without this parameter, the default is 1 minute and will automatically close.
        proxy_read_timeout 900s;
    }

Last edited by vincix; 04-17-2019 at 08:27 AM.
 
1 members found this post helpful.
Old 04-17-2019, 09:09 AM   #6
tyler2016
Member
 
Registered: Sep 2018
Distribution: Debian, CentOS, FreeBSD
Posts: 243

Rep: Reputation: Disabled
Quote:
Originally Posted by vincix View Post

I don't understand why it wouldn't work with an Alias and/or with another Location for /.well-known. I tried it, but it doesn't seem to have any effect. I added the following before <Location />
I'm glad you got it to work with Nginx. It has been a while since I have used Let's Encrypt for a machine on my network. Right now, I am only using Let's Encrypt for shared hosting. I would think adding another location block like you said would work. I've never used Apache for reverse proxying, I've always used HAProxy.

I just thought of something you might be able to do if you really want to use Apache. I wonder if it would work if you were to configure a virtual host for Let's Encrypt, then setup another location block that uses it as a back end?
 
Old 04-17-2019, 04:27 PM   #7
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
Maybe I was a little bit misleading. I had the solution for nginx from the very beginning, but it's important to find it on apache, because this is what we're currently using in some setups and there are still many configurations that I find a little bit dificult to transfer to nginx - I still need to understand how I would go about doing that.
So basically two <VirtualHost> sections, one for let's encrypt and the other for the backend? I don't see how anything good could come out of this, but I could give it a try, I guess. I expect that the first one takes precedence over the second one, and so the second wouldn't be accessible anymore.

Last edited by vincix; 04-17-2019 at 04:32 PM.
 
Old 07-09-2019, 05:15 AM   #8
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
I ended up writing a Redirect exception on the 80 VirtualHost, so that letsencrypt simply doesn't connect to the 443 at all:
Code:
RedirectMatch 301 ^(?!/\.well-known/acme-challenge).* https://example.com$0
 
Old 07-10-2019, 07:38 AM   #9
Hostech_Support
Member
 
Registered: Oct 2017
Location: India
Posts: 41

Rep: Reputation: Disabled
I’d suggest changing the rewrite rule at the end of the apache conf (http section ) to;

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Last edited by Hostech_Support; 07-10-2019 at 08:20 AM.
 
Old 07-11-2019, 03:16 PM   #10
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
If you also gave some arguments and explained to me how that could possibly accomodate the letsencrypt exception, that would be really amazing.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: How to Setup Apache Subversion with HTTPS Letsencrypt on CentOS 7 LXer Syndicated Linux News 0 09-12-2018 09:31 PM
[SOLVED] unable to install letsencrypt certificate with apache vincix Linux - Server 2 10-25-2016 09:45 AM
Nginx Reverse proxy on a internal apache reverse server ITiger Linux - Software 0 04-25-2014 07:44 AM
help createing exception class from base STL exception qwijibow Programming 4 04-20-2005 05:23 AM
Runtime Exception vs. Exception mikeshn Programming 1 09-22-2002 05:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 02:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration