How to set up a reverse proxy in IPFire for Noobs?

I’m trying to follow the tutorial that the ipfire wiki has to configure a reverse proxy, but I can’t understand it, there is a resource that explains the step by step in more detail.

PS, I’m a Noob for this, I’ve been starting to understand how to configure a firewall.

1 Like

This tutorial comes to you directly from ChatGPT and myself. My contributions? The mistakes you will inevitably have to correct. Well, you have to do some work, don’t you? Find out where I involuntarily introduced errors and correct them. Look at the logs and the logic of these configuration examples. Or ask a real expert, or the AI in question.


Premise

The purpose of running nginx as a Reverse-Proxy (rPROXY) is to receive traffic (frequently web traffic, but it can be anything) destined to IPFire, but with the intent to sort it to different local machines according to specific rules. For example, traffic coming from Internet from different domain names, destined to different web server running behind behind IPFire in the orange zone (here it will be 192.168.2.1/24).

Those servers, in nginx parlance, are called Upstream and are the back-end of nginx. This is explained by the definition of rPROXY, which is a server that sits between a client and one or more servers. It receives requests from clients and forwards them to the appropriate server based on rules configured by an administrator. Here the client is on the Internet side, and the server is on the LAN side. The response from the server is then returned to the client through the reverse proxy. In this way, the client interacts with the reverse proxy as if it were the actual server, while the reverse proxy handles the details of connecting to the backend servers.


Steps common to all use case

This tutorial will present several use-cases. In common to all of them, you do this:

  1. Using pakfire install nginx in IPFire.
  2. as a root, modify nginx configuration:
nano /etc/nginx/nginx.conf
  1. see specific cases;
  2. test the configuration file to see if there are syntactic mistake:
nginx -t
  1. restart nginx:
/etc/init.d/nginx restart

Case Number 1

Now, let’s see the first situation. You have a domain name registered to a DNS provider, we will call it example.com which points to your IPFire public IP address.

You want the traffic for http://www.exaple.com/blog to go to the web-server running on the IP address 192.168.2.100 port 3000 and http://www.example.com/nextcloud to go to 192.168.2.101 port 4000, you write this configuration file:

http {
    server {
        listen 80;
        location /blog {
            proxy_pass http://192.168.2.100:3000;
        }
        location /nextcloud {
            proxy_pass http://192.168.2.101:4000;
        }
    }
}

Case Number 2

Second scenario. As case number 1, but this time you want also to sort the encrypted traffic for the /nextcloud and /blog web applications. To enable encrypted traffic on port 443, you need to configure SSL certificates for the domain name. You can obtain SSL certificates from Let’s Encrypt or purchase them from a commercial CA.

Once you have obtained the SSL certificate, you need to configure NGINX to use it. You can do this by creating a directory to store the SSL certificate and key, and then adding the following lines to the NGINX configuration file:

ssl_certificate /path/to/example.com.crt;
ssl_certificate_key /path/to/example.com.key;

Replace “/path/to/example.com.crt” and “/path/to/example.com.key” with the path to your SSL certificate and key files for the domain name example.com.

Now you need to configure NGINX to act as a reverse proxy for each of the domain names. You can do this by creating a server block in the NGINX configuration file listing two locations, one for each termination of the url. For example:

http {
   server {
       listen 80;
       listen [::]:80;
       server_name example.com;
       return 301 https://$server_name$request_uri;
   }

   server {
       listen 443 ssl;
       listen [::]:443 ssl;
       server_name example.com;
       ssl_certificate /path/to/example.com.crt;
       ssl_certificate_key /path/to/example.com.key;

       location  /blog {
           proxy_pass http://192.168.2.100:80;
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       }
       location  /nextcloud {
           proxy_pass http://192.168.2.101:80;
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       }
   }
}

In this example, we create a server block for the domain name example.com that listens on both port 80 and port 443. The “return 301” line redirects all HTTP traffic to HTTPS. The “ssl_certificate” and “ssl_certificate_key” lines specify the SSL certificate and key files for the domain name.

The location /nextcloud and location /blog blocks specifies the location where requests will be proxied to. In this case, we’re proxying requests to a web server running on IP address 192.168.2.100 and 101 respectively at port 80. You can replace this with the IP address and port of the web server that you want to proxy traffic to.

The “listen 80;” directive instructs NGINX to listen for incoming connections on port 80 of all available IP addresses, both IPv4 and IPv6.

The “listen [::]:80;” directive instructs NGINX to listen for incoming connections on port 80, but only for IPv6 connections. The “[::]” syntax represents the unspecified IPv6 address, which means that NGINX will listen for connections on all available IPv6 addresses.

So, the main difference between “listen 80;” and “listen [::]:80;” is that the former listens on both IPv4 and IPv6 addresses, while the latter listens only on IPv6 addresses. Given that the current version of IPFire will not work with IPv6, this directive can be omitted.


Case Number 3

Here you want to send traffic from two different domains, where example1.com goes to 192.168.2.100 and example2.com goes to 192.168.2.101, in the root directory (no /nextcloud or /blog here). Each domain has its own certificates. The two servers are running on port 80 but the traffic will be redirected to port 443.

http {
    upstream example1 {
        server 192.168.2.100:80; # replace with your server IP
    }

    upstream example2 {
        server 192.168.1.101:80; # replace with your server IP
    }

    server {
        listen 80;
        server_name example1.com www.example1.com example2.com www.example2.com;
        return 301 https://$host$request_uri;
    }

    server {
        listen 443 ssl;
        server_name example1.com www.example1.com;

        ssl_certificate /etc/letsencrypt/live/example1.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example1.com/privkey.pem;

        location / {
            proxy_pass http://example1;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }

    server {
        listen 443 ssl;
        server_name example2.com www.example2.com;

        ssl_certificate /etc/letsencrypt/live/example2.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example2.com/privkey.pem;

        location / {
            proxy_pass http://example2;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }
}

Here we used the upstream directive to simplify the namespace to be used in the location directive.


Case number 4

Here’s a tutorial on how to set up a reverse proxy for the domain example.com to load balance on two servers using NGINX, with traffic on port 80 redirected to port 443, all in one configuration file called nginx.conf.

http {
  upstream example {
    server 192.168.2.100;
    server 192.168.2.101;
    }

  server {
    listen 80;
    server_name example.com;
    return 301 https://$server_name$request_uri;
    }

  server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;

    location / {
      proxy_pass http://example;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto https;
      }
    }
}

In the above example, the upstream block defines the two servers that NGINX will load balance traffic to. The server block listening on port 80 redirects all traffic to port 443 using a 301 redirect. Finally, the server block listening on port 443 is where we define the SSL certificate paths and set up the reverse proxy to the example upstream server.

4 Likes

@jon Should I put this (long) tutorial on the wiki? Or it does not belong there? Of course, after I test the correctness of the code (which I would greatly welcome other people testing them).

I am not a big fan of ChatGPT. I’ve seen a few examples of bad code (the code works but just not the right way to do something).

As long as the code is well tested it should be OK to add to the Wiki.

3 Likes

I think using ChatGPT ( and other AIs ) breaks our working style.
The documentation in the wiki is by humans with knowledge of the IPFire system. The feasibility, that each registered user can edit it, does not really reduce quality. Each change is reported to a group of developers or ‘dev helpers’, which reviews this modification.
Texts generated by AIs are IMO to complex to check truth, if not just undecidable.

1 Like

I think you might not see fully what is happening here, as I was few days ago. This thing is unstoppable and as I said it will be google search on steroids. The only reason you know that I used ChatGPT here is because I have a code of ethics that I follow very strictly. I did not copy and paste, I checked everything on the best of my ability and modified the text with the intent of improving it, not hiding its origin, and I said what I did.

I am researching the topic of having a reverse proxy since January. I read everything I could find using my four search engines, as I usually do. Had I use ChatGPT I could have shorten this research effort and time by a factor of 10, at the very least. I also see the limitations and the danger of it, as I saw how the algorithms makes stuff up when the training data are insufficient. However remember this (and I repeat myself): this thing cannot be stopped. You can only embrace it and try to use it in a way that makes sense to you. But you cannot forbid it, as Italy is trying to do. None can stop a market force when a new technology aligns with the incentives of the participant, especially when it is driven by the network effect, like Internet was.

So, at the next call with the project, I suggest you discuss a policy for the best interest of the project itself. To do that you need to test it personally and thoroughly, as I did it.

At the moment I will keep using it in my answers until I am told otherwise. I won’t touch the wiki until I receive a clear guideline. But, remember (and again, I repeat myself) I will keep using this thing because it is too useful for me. If this is against the project guidelines, I will not participate in this forum anymore or touch the wiki. I think this is the best ethical policy for myself and I said, I try very hard to strictly adhere to it.

I don’t believe that anyone is going to tell you to stop doing that.

The expectation of everyone on the forum is that they make best efforts to give good answers. If ChatGPT helps in doing that then I don’t see the problem with it. Google search or DuckDuck search or Bing search etc are all allowed, as is getting information from a book and providing it.

People can also currently provide input to solve a problem that is totally incorrect based on a Google search, if they don’t look through what they are providing.

As long as what you put into the wiki is correct in terms of the instructions being provided, then I don’t see a problem if it is based on ChatGPT as opposed to info coming from a Google search.

If someone copy and pastes info into the wiki from a Google search, that is incorrect or unsafe to follow then that input will be reverted. I have seen a few inputs in the wiki that have been reverted over the last few years but generally most people take care with what they are entering to make sure it is correct and in line with the ethos of IPFire. If ChatGPT can help with that, then fine, as far as I am concerned.

5 Likes

Thank you for the clarification @bonnietwin Adolf. I agree with your very balanced and well thought approach.

The way I see AI changing this part of the IPFire project is the following: any question that should merit a thoughtful answer should clearly show a familiarity with the official documentation and a best effort to solve the problem by doing research, with search engines and now AI. The latter having 100X more power than the former. In other words, did you:

  • Read the WIKI?
  • Search the forum?
  • searched for answers with search engines?
  • asked an AI?

The language barrier now is mostly a self inflicted mental block as well, as the AI like DeepL is extremely good. Also ChatGPT can speak many languages. What I will do from now on for any generic question similar to the one opening this thread is to check with the AI to see if the answer is good (in my opinion of course) and then invite the posters to do their own search with that tool. There is no reason to spend time trying to explain something that can be explained in seconds with a better English then my own. Also, I am NOT going to blindly post a cut and pasted response on behalf of the posters.

3 Likes

@cfusco ,
I think I just must clarify my formerly posted opinion.

I do not think, ChatGPT isn’t a way to sample informations about a theme. It is much quicker in searching that. But like doing a search with search engines, you have to filter good and bad informations ( original versus meta-(meta-…) information.
And yes, it is the ‘reputation’ of the poster of information gather by ChatGPT, which influences the acknowledgement by the ‘core community’. In case of unknown posters the effort of verification is just increased because of the greater amount of unknown sources.

Basically, ChatGPT can help to expand the ressources of ‘noobs articles’ in the wiki.

4 Likes

@bbitsch I cannot agree more. I clearly did not understand your answer fully and given that you spelled it out quite well, it is my comprehension that failed before. Well, the important thing is that now I got it.

2 Likes