HOW-TO: Set up NGINX to reverse proxy your jails w/ Certbot

AirborneTrooper

Contributor
Joined
Jun 20, 2014
Messages
148
Anyone get Organizr API v2.1 working? I am looking for some help.

In case anyone else stumbles across this, you need to edit inside the organizr jail

Code:
nano /usr/local/etc/nginx/nginx.conf

Replace the config with: 

user www;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
root /usr/local/www/Organizr;
location / {
index index.php index.html index.htm;
}
location /api/v2 {
        try_files $uri /api/v2/index.php$is_args$args;
} 
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/www/nginx-dist;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
}
 
Last edited:

AirborneTrooper

Contributor
Joined
Jun 20, 2014
Messages
148
I've been having trouble with SABNZBD opening in Organizr tab after over a year of it working fine. Here's what I did to make it work. In the proxy_setup.conf file, change the part for /sabnzbd to this:

Code:
location /sabnzbd { proxy_pass http://yoursabnzbdiphere:8080;
proxy_redirect  http://  $scheme://; proxy_send_timeout 240;
proxy_set_header    Connection            "upgrade";
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-Host    $server_name;
proxy_set_header    X-Forwarded-Proto    https;
proxy_set_header    X-Forwarded-Ssl        on;
proxy_set_header    Upgrade                $http_upgrade;
}


Once you've saved it run a
Code:
service nginx reload
 

adrianwi

Guru
Joined
Oct 15, 2013
Messages
1,231
Setting this up and getting an understanding of how it all works is really good, but NGINX Proxy Manager is a much simpler solution and can be run in a very lightweight VM or even a Raspberry Pi.
 

AirborneTrooper

Contributor
Joined
Jun 20, 2014
Messages
148
Setting this up and getting an understanding of how it all works is really good, but NGINX Proxy Manager is a much simpler solution and can be run in a very lightweight VM or even a Raspberry Pi.

You're probably right but with this there is no need for another Pi (I already have 1 for pi-hole and 2 for OctoPrint servers for my 2 3d printers) and don't need to spin up a VM. This is always on as long as my TrueNAS box is up.
 

adrianwi

Guru
Joined
Oct 15, 2013
Messages
1,231
You're probably right but with this there is no need for another Pi (I already have 1 for pi-hole and 2 for OctoPrint servers for my 2 3d printers) and don't need to spin up a VM. This is always on as long as my TrueNAS box is up.

Yeah, there are so many different ways to roll this stuff! I've got Pi-hole running in a small VM on my FreeNAS box and then NPM running as a plugin on an RPi I primarily use for Home Assistant. I did have NPM running on a small VM too, but it works just as well on the Pi.
 

AirborneTrooper

Contributor
Joined
Jun 20, 2014
Messages
148
Easy enough, go into your nginx jail, cd to certbot and run certbot renew. That's it. Updated for the next 3 months
 

TheNASnovice

Dabbler
Joined
Feb 2, 2021
Messages
20
Thanks for that great HowTo.
After some daunting HowTos finally a good one that worked for me.

But there is a problem for me using nextcloud with this reverse proxy.

Here the status for now:

When I visit:
my-sub.selfhost.co/nextcloud

("my-sub" is not the real subdomain but I think I shouldn't write the real one here ;-) )
I get to my nextcloud jail and it seems to be encrypted.
The URL now says:

But nextcloud itself has a "truted-domain-check" and in the browser I have to read then:
"Please contact your administrator. If you are an administrator, edit the "trusted_domains" setting in config / config.php. See example in config / config.sample.php."


Looking into the config.php I tried ALL I can imagine (for sure most of it has to be deleted after successful installation).
Have a look here please:

Code:
<?php
$CONFIG = array (
  'instanceid' => 'thisIsTheID',
  'passwordsalt' => 'hereIsTheSalt',
  'secret' => 'hereIsASecret',
  'trusted_domains' =>
  array (
    0 => 'my-sub.selfhost.co:8080',
    1 => '192.168.2.205',
    2 => 'localhost',
    3 => 'my-sub.selfhost.co/nextcloud',
    4 => 'my-sub.selfhost.co:80/nextcloud',
    5 => 'my-sub.selfhost.co:443/nextcloud',
    6 => 'my-sub.selfhost.co:80',
    7 => 'my-sub.selfhost.co:443',
    8 => '192.168.2.205:80',
    9 => '192.168.2.205:443',
   10 => 'https://my-sub.selfhost.co/nextcloud',
   11 => 'https://my-sub.selfhost.co:80/nextcloud',
   12 => 'https://my-sub.selfhost.co:443/nextcloud',
   13 => '192.168.2.206:80',
   14 => '192.168.2.206:443',
   15 => '192.168.2.206',
  ),
  'datadirectory' => '/mnt/data',
  'dbtype' => 'mysql',
  'version' => '20.0.6.1',
  'overwrite.cli.url' => 'http://my-sub.selfhost.co:80',
  'dbname' => 'nextcloud',
  'dbhost' => 'localhost:/tmp/mysql.sock',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'mysql.utf8mb4' => true,
  'dbuser' => 'db_admin',
  'dbpassword' => 'mydbPassword',
  'installed' => true,
  'redis' =>
  array (
    'host' => '/var/run/redis/redis.sock',
    'port' => 0,
  ),
  'memcache.local' => '\\OC\\Memcache\\APCu',
  'memcache.locking' => '\\OC\\Memcache\\Redis',
  'app_install_overwrite' =>
  array (
    0 => 'occweb',
  ),
  'mail_smtpmode' => 'smtp',
  'mail_smtpsecure' => 'ssl',
  'mail_sendmailmode' => 'smtp',
  'mail_from_address' => 'my.name',
  'mail_domain' => 'yyy.org',
  'mail_smtpauth' => 1,
  'mail_smtpauthtype' => 'LOGIN',
  'mail_smtphost' => 'smtp.yyy.org',
  'mail_smtpport' => '465',
  'mail_smtpname' => 'xxx@yyy.org',
  'mail_smtppassword' => 'myPassword',
);


192.168.2.205 is the nextcloud jail
192.168.2.206 is the reverse-proxy jail

I can't figure out what to add so that nextcloud accepts the domain I come from as trusted.

May someone can help? Would be great!
 

TheNASnovice

Dabbler
Joined
Feb 2, 2021
Messages
20
I noticed something else:
It seems that my nextcloud is accessible so far but something is wrong with the CSS Style.
I get the error message just in a blank page (please have a look at the picture attached).

In Post #200 I found some additions for the config.php so that it looks now like this (CSS problem and "trusted-domain-problem" are still there):

Code:
<?php
$CONFIG = array (
  'instanceid' => 'thisIsTheID',
  'passwordsalt' => 'hereIsTheSalt',
  'secret' => 'hereIsASecret',
  'trusted_domains' =>
  array (
    0 => 'my-sub.selfhost.co:8080',
    1 => '192.168.2.205',
    2 => 'localhost',
    3 => 'my-sub.selfhost.co/nextcloud',
    4 => 'my-sub.selfhost.co:80/nextcloud',
    5 => 'my-sub.selfhost.co:443/nextcloud',
    6 => 'my-sub.selfhost.co:80',
    7 => 'my-sub.selfhost.co:443',
    8 => '192.168.2.205:80',
    9 => '192.168.2.205:443',
   10 => 'https://my-sub.selfhost.co/nextcloud',
   11 => 'https://my-sub.selfhost.co:80/nextcloud',
   12 => 'https://my-sub.selfhost.co:443/nextcloud',
   13 => '192.168.2.206:80',
   14 => '192.168.2.206:443',
   15 => '192.168.2.206',
  ),
  'default_language' => 'de',
  'default_locale' => 'de_DE',
  'datadirectory' => '/mnt/data',
  'dbtype' => 'mysql',
  'version' => '20.0.6.1',
  'overwrite.cli.url' => 'https://my-sub.selfhost.co/nextcloud',
  'overwritewebroot' => '/nextcloud',
  'dbname' => 'nextcloud',
  'dbhost' => 'localhost:/tmp/mysql.sock',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'mysql.utf8mb4' => true,
  'dbuser' => 'db_admin',
  'dbpassword' => 'mydbPassword',
  'installed' => true,
  'redis' =>
  array (
    'host' => '/var/run/redis/redis.sock',
    'port' => 0,
  ),
  'memcache.local' => '\\OC\\Memcache\\APCu',
  'memcache.locking' => '\\OC\\Memcache\\Redis',
  'app_install_overwrite' =>
  array (
    0 => 'occweb',
  ),
  'mail_smtpmode' => 'smtp',
  'mail_smtpsecure' => 'ssl',
  'mail_sendmailmode' => 'smtp',
  'mail_from_address' => 'my.name',
  'mail_domain' => 'yyy.org',
  'mail_smtpauth' => 1,
  'mail_smtpauthtype' => 'LOGIN',
  'mail_smtphost' => 'smtp.yyy.org',
  'mail_smtpport' => '465',
  'mail_smtpname' => 'xxx@yyy.org',
  'mail_smtppassword' => 'myPassword',
);


I hope someone can help me out... it is frustrating to try for days :frown:.
 

Attachments

  • 2021-02-03 08_45_40-.png
    2021-02-03 08_45_40-.png
    296.7 KB · Views: 296

nrgpix

Dabbler
Joined
Sep 3, 2020
Messages
10
Disclaimer:
-I'm making this guide simply to help other people, i just put together multiple guides found on the internet (which i'll post below). This is meant to be as easy as it gets for a newbie to get NGINX to reverse proxy using https.


-What is a reverese proxy: https://en.wikipedia.org/wiki/Reverse_proxy
-Using certbot with nginx: https://hypatiasoftware.org/2016/07/06/httpsssl-made-easy-with-certbot/
-Thank you for the guide, not only i used it, i stole the template to write this one: https://forums.freenas.org/index.php?threads/how-to-nextcloud-10-w-apache-php-and-mariadb.46111/
-Certbot instructions: https://certbot.eff.org/all-instructions/#freebsd-nginx


The Goal:

The ultimate goal is to have one domain and redirect the requests simply using "mydomain.com/cloud ; mydomain.com/couchpotato ; /sickrage; etc. "
In order to do so, we will have to get NGINX up and running, use certbot to obtain a certificate, set up nginx to use this certificate, set up nginx to redirect to the appropriate jails. Keep in mind that having a reverse proxy allow you to have some kind of "shield" before jails using simple http, and gives all those jails (in our case but those could ultimately be other servers) anonymity.

Getting Started -
General Assumptions:

  • You know how the get a domain
  • You are using FreeNAS 9.10.2-RELEASE with FreeBSD Jail Template "------"
  • Your FreeNAS is connected to the internet, and your jails are properly configured.
  • You know how to use nano.
Specifics:
My Build:
ASRock E3C224
Xeon e3-1231v3
16GB Crucial (2x8gb) ECC DDR3-1600
x6 3TB Western Digital Red


Let's Begin

Creating the Jail

Create Jail
Jails > Add Jail

  • Jail Name: nginx_reverse
  • Template: "------"
  • IPv4 Address: 192.168.1.99
  • Leave all else default


SSH to your Jail
Open bitvise/putty or whatever you're using to SSH to you Freenas machine:
  1. Let's find our Jail
    jls
  2. Select the Jail we created earlier
    jexec n. of the jail tcsh
Install the pkg we need
  1. Get it ready with everything we need
    pkg update && pkg upgrade
  2. Install nano, NGINX, git and python
    pkg install nano nginx git python
Get Certbot and get you certs
  1. Clone Certbot
    git clone https://github.com/certbot/certbot.git
  2. Now let's obtain our Certificate, Certbot will automatically install any system dependencies and create a Python Virtual Environment to manage any Python packages it requires. Remeber that nothing has to be using ports 80 and 443 during the next step and that they have to be open for our jail!
    cd certbot
    ./letsencrypt-auto --debug certonly --standalone -d example.com -d www.example.com change those to your domain and/or delete the one you're not using, for example i didn't need www.example.com , so i just deleted -d www.example.com.
Generate your own strong and unique Diffie-Hellman (DH) Key
  1. cd /usr/local/etc/ssl/
  2. openssl dhparam -out dhparams.pem 4096
Create ssl_common.conf
  1. We are going to store all our security settings in this file. I am no expert and i find the following settings to simply work. I'll be more than happy to edit the whole guide, if necessary, to adapt it to a more clean and secure way.
    cd /usr/local/etc/nginx/
    nano ssl_common.conf
  2. Simply copy and paste all the following but remember to modify the domain name!
    Code:
    # Thanks to https://cipherli.st/ for providing a great reference! Please check out their site
    # to make sure your SSL Configuration is up to date with current standards! Be aware that in this
    # example we use a slightly liberal cipherlist to allow for older browsers on older devices, Eg.
    # IE8, android 2.4, etc
    # Enable Perfect Forward Secrecy (PFS)
    ssl_prefer_server_ciphers on;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    # Disable SSLv2 and SSLv3 (BEAST and POODLE attacks)
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    # Enable our strong DH Key
    ssl_dhparam /usr/local/etc/ssl/dhparams.pem;
    # Cipher-list for PFS.
    ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:ECDHE-RSA-AES128-GCM-SHA256:AES256+EECDH:DHE-RSA-AES128-GCM-SHA256:AES256+EDH:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
    ssl_ecdh_curve secp384r1;
    # Requires nginx >= 1.1.0
    ssl_session_cache shared:SSL:10m;
    ssl_session_tickets off;
    # Requires nginx >= 1.5.9
    ssl_stapling on;
    # Requires nginx >= 1.3.7
    ssl_stapling_verify on;
    # Requires nginx => 1.3.7
    resolver 8.8.8.8 4.4.4.4 valid=300s;
    resolver_timeout 5s;
    # HSTS Support
    add_header Strict-Transport-Security "max-age=63072000;includeSubdomains; preload";
    # These headers can break applications, be careful!
    add_header X-Frame-Options DENY;
    add_header X-Content-Type-Options nosniff;
    
Setup NGINX
  1. We need to enter the conf file of NGINX and set it up to use all of this.
    nano nginx.conf
  2. Modify the bit after http to look something like this ( we are going to setup the "proxy_setup.conf" after this step ). Always remember to change the domain name.
    Code:
    listen 443 ssl;
            server_name  example.com;
            include ssl_common.conf;
            include proxy_setup.conf;
    
  3. Create the proxy_setup.conf
    nano proxy_setup.conf
  4. Here it is very easy for you to understand what you have to do, i'm going to post examples but those are not going to work with you. You will have to switch the bits to what are your actual Jails local IPs and what you want the proxy_pass to be.
Code:
     location /sabnzbd {
     proxy_pass http://192.168.192.200:8080;
     proxy_redirect off;
     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 /sonarr {
     proxy_pass http://192.168.192.202:8989;
     proxy_redirect off;
     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 /couchpotato {
     proxy_pass http://192.168.192.201:5050;
     proxy_redirect off;
     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 /headphones {
     proxy_pass http://192.168.192.203:8181;
     proxy_redirect off;
     proxy_set_header Host $host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     }



Check that everything is working
  1. service nginx onestart
  2. Now everything should be up and running. Check if it is, going to example.com/sonarr. You should see https being used.
  3. service nginx onestop

  4. sysrc nginx_enable=YES
  5. service nginx start

EXTRA
Keep in mind you will have to tweak a few things in sickrage (for example) to make it work. In my case changing the webroot or the base URL did the trick just fine since NGINX was "breaking" the URLs without that set.
Another advice: when changing the proxy setup file, use the command service nginx reload, this way you will avoid downtime in case you made mistakes, since nginx won't load the new file but keep using the old (and working) one.

Renew your certificate
  1. -OPTIONAL- I don't like using vi as an editor, therefore i'm going to switch to nano for crontab as well
    setenv VISUAL /usr/local/bin/nano
  2. crontab -e
  3. Code:
    30 1 * * 1 ./certbot/letsencrypt-auto renew --pre-hook "service nginx stop" --post-hook "service nginx start"



Test SSL Labs, you should now see an A+!
OOO things have moved on a little ... I fall over on "./letsencrypt" in TrueNAS-12.0-U8. Looking upo stackexcnagea dn the blogs here I cannot get past this blocker any input gratefully recieved
 

thijsjek

Dabbler
Joined
Aug 12, 2017
Messages
19
Maybe not the smartest way to use a free as9.8 Guide for 12, Or resurrect an old thread.
OOO things have moved on a little ... I fall over on "./letsencrypt" in TrueNAS-12.0-U8. Looking upo stackexcnagea dn the blogs here I cannot get past this blocker any input gratefully recieved
Today you can install certbot by :
pkg search certbot
pkg install pyXX-certbot (fill in the value you find after first command).
 
Top