nginx.md (7774B)
1 # NGINX 2 3 In order to use NGINX as a reverse proxy for GoToSocial you'll need to have it installed on your server. If you intend for the NGINX instance to also handle TLS, you'll need to [provision TLS certificates](../../advanced/certificates.md) too. 4 5 NGINX is [packaged for many distributions](https://repology.org/project/nginx/versions). It's very likely you can install it with your distribution's package manager. You can also run NGINX using a container runtime with the [official NGINX image](https://hub.docker.com/_/nginx) that's published to the Docker Hub. 6 7 In this guide we'll also show how to use certbot to provision the TLS certificates. It too is [packaged in many distributions](https://repology.org/project/certbot/versions) but many distributions tend to ship fairly old versions of certbot. If you run into trouble it may be worth considering using the [container image](https://hub.docker.com/r/certbot/certbot) instead. 8 9 ## Configure GoToSocial 10 11 If GoToSocial is already running, stop it. 12 13 ```bash 14 sudo systemctl stop gotosocial 15 ``` 16 17 Or if you don't have a systemd service just stop it manually. 18 19 Tweak your GoToSocial configuration like so: 20 21 ```yaml 22 letsencrypt-enabled: false 23 port: 8080 24 bind-address: 127.0.0.1 25 ``` 26 27 The first setting disables the built-in provisioning of TLS certificates. Since NGINX will now be handling that traffic GoToSocial no longer needs to be bound to port 443, or any privileged port. 28 29 By setting the `bind-address` to `127.0.0.1` GoToSocial will no longer be accessible directly from the outside. If your NGINX and GoToSocial instance aren't running on the same server you'll need to bind to an IP address that lets your reverse proxy reach your GoToSocial instance. By binding to a private IP address you can be sure GoToSocial can't be accessed except through NGINX. 30 31 ## Set up NGINX 32 33 First we will set up NGINX to serve GoToSocial as unsecured http and then use Certbot to automatically upgrade it to serve https. 34 35 Please do not try to use it until that's done or you'll risk transmitting passwords over clear text, or breaking federation. 36 37 First we'll write a configuration for NGINX and put it in `/etc/nginx/sites-available`. 38 39 ```bash 40 sudo mkdir -p /etc/nginx/sites-available 41 sudoedit /etc/nginx/sites-available/yourgotosocial.url.conf 42 ``` 43 44 In the above commands, replace `yourgotosocial.url` with your actual GoToSocial host value. So if your `host` is set to `example.org`, then the file should be called `/etc/nginx/sites-available/example.org.conf` 45 46 The file you're about to create should look like this: 47 48 ```nginx 49 server { 50 listen 80; 51 listen [::]:80; 52 server_name example.org; 53 location / { 54 # set to 127.0.0.1 instead of localhost to work around https://stackoverflow.com/a/52550758 55 proxy_pass http://127.0.0.1:8080; 56 proxy_set_header Host $host; 57 proxy_set_header Upgrade $http_upgrade; 58 proxy_set_header Connection "upgrade"; 59 proxy_set_header X-Forwarded-For $remote_addr; 60 proxy_set_header X-Forwarded-Proto $scheme; 61 } 62 client_max_body_size 40M; 63 } 64 ``` 65 66 Change `proxy_pass` to the ip and port that you're actually serving GoToSocial on (if it's not on `127.0.0.1:8080`), and change `server_name` to your own domain name. 67 68 If your domain name is `example.org` then `server_name example.org;` would be the correct value. 69 70 If you're running GoToSocial on another machine with the local ip of 192.168.178.69 and on port 8080 then `proxy_pass http://192.168.178.69:8080;` would be the correct value. 71 72 **Note**: You can remove the line `listen [::]:80;` if your server is not ipv6 capable. 73 74 **Note**: `proxy_set_header Host $host;` is essential. It guarantees that the proxy and GoToSocial use the same server name. If not, GoToSocial will build the wrong authentication headers, and all attempts at federation will be rejected with 401. 75 76 **Note**: The `Connection` and `Upgrade` headers are used for WebSocket connections. See the [WebSocket docs](websocket.md). 77 78 **Note**: `client_max_body_size` is set to 40M in this example, which is the default max video upload size for GoToSocial. You can make this value larger or smaller if necessary. The nginx default is only 1M, which is rather too small. 79 80 **Note**: To make `X-Forwarded-For` and rate limiting work, set the `trusted-proxies` configuration variable. See the [rate limiting](../../api/ratelimiting.md) and [general configuration](../../configuration/general.md) docs 81 82 Next we'll need to link the file we just created to the folder that nginx reads configurations for active sites from. 83 84 ```bash 85 sudo mkdir -p /etc/nginx/sites-enabled 86 sudo ln -s /etc/nginx/sites-available/yourgotosocial.url.conf /etc/nginx/sites-enabled/ 87 ``` 88 89 Again, replace `yourgotosocial.url` with your actual GoToSocial host value. 90 91 Now check for configuration errors. 92 93 ```bash 94 sudo nginx -t 95 ``` 96 97 If everything is fine you should get this as output: 98 99 ```text 100 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 101 nginx: configuration file /etc/nginx/nginx.conf test is successful 102 ``` 103 104 Everything working? Great! Then restart nginx to load your new config file. 105 106 ```bash 107 sudo systemctl restart nginx 108 ``` 109 110 ## Set up TLS 111 112 !!! note 113 We have additional documentation on how to [provision TLS certificates](../../advanced/certificates.md) that also provides links to additional content and tutorials for different distributions that may be good to review. 114 115 You should now be able to run certbot and it will guide you through the steps required to enable https for your instance. 116 117 ```bash 118 sudo certbot --nginx 119 ``` 120 121 After you do, it should have automatically edited your configuration file to enable https. 122 123 Reload NGINX one last time: 124 125 ```bash 126 sudo systemctl restart nginx 127 ``` 128 129 Now start GoToSocial again: 130 131 ```bash 132 sudo systemctl start gotosocial 133 ``` 134 135 ## Security hardening 136 137 If you want to harden up your NGINX deployment with advanced configuration options, there are many guides online for doing so ([for example](https://beaglesecurity.com/blog/article/nginx-server-security.html)). Try to find one that's up to date. Mozilla also publishes best-practice SSL configuration [here](https://ssl-config.mozilla.org/). 138 139 ## Results 140 141 You should now be able to open the splash page for your instance in your web browser, and will see that it runs under https! 142 143 If you open the NGINX config again, you'll see that Certbot added some extra lines to it. 144 145 !!! note 146 This may look a bit different depending on the options you chose while setting up Certbot, and the NGINX version you're using. 147 148 ```nginx 149 server { 150 server_name example.org; 151 location / { 152 # set to 127.0.0.1 instead of localhost to work around https://stackoverflow.com/a/52550758 153 proxy_pass http://127.0.0.1:8080; 154 proxy_set_header Host $host; 155 proxy_set_header Upgrade $http_upgrade; 156 proxy_set_header Connection "upgrade"; 157 proxy_set_header X-Forwarded-For $remote_addr; 158 proxy_set_header X-Forwarded-Proto $scheme; 159 } 160 client_max_body_size 40M; 161 162 listen [::]:443 ssl ipv6only=on; # managed by Certbot 163 listen 443 ssl; # managed by Certbot 164 ssl_certificate /etc/letsencrypt/live/example.org/fullchain.pem; # managed by Certbot 165 ssl_certificate_key /etc/letsencrypt/live/example.org/privkey.pem; # managed by Certbot 166 include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot 167 ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot 168 } 169 170 server { 171 if ($host = example.org) { 172 return 301 https://$host$request_uri; 173 } # managed by Certbot 174 175 listen 80; 176 listen [::]:80; 177 server_name example.org; 178 return 404; # managed by Certbot 179 } 180 ``` 181 182 A number of additional configurations for nginx, including static asset serving and caching, are documented in the [Advanced](../../advanced/index.md) section of our documentation.