Wiki source code of Nginx: useful configuration directives
Last modified by Alexandru Pentilescu on 2023/06/25 18:58
Hide last authors
author | version | line-number | content |
---|---|---|---|
![]() |
1.1 | 1 | (% class="jumbotron" %) |
2 | ((( | ||
3 | (% class="container" %) | ||
4 | ((( | ||
5 | = Nginx: useful configuration directives = | ||
6 | This article will function along the lines of a //Tips and tricks//-style article, where I'll be showing off various configuration directives for nginx to use in the wild to achieve specific purposes. | ||
7 | Hopefully, this article will grow over time. | ||
8 | ))) | ||
9 | ))) | ||
10 | |||
11 | (% class="row" %) | ||
12 | ((( | ||
13 | (% class="col-xs-12 col-sm-8" %) | ||
14 | ((( | ||
15 | |||
16 | = Mapping a specific URL location to a directory location on the system = | ||
17 | |||
![]() |
6.1 | 18 | Sometimes, you may want a very simple configuration where you have a specific URL location (i.e. **https://transistor.one/bin/ **) and you want this URL prefix to indicate to Nginx to use a specific directory on the system to look for the requested files in. Or, in other words, every HTTP GET request sent to **https://transistor.one/bin/ ** (e.g. **https://transistor.one/bin/folder1/myfile.png **) to be searched for in a specific systems directory (e.g. **/var/www/static/**). This way, a request for **https://transistor.one/bin/folder1/myfile.png ** should resolve to **/var/www/static/folder1/myfile.png**. How do we do this? |
![]() |
1.1 | 19 | |
20 | Simple! We use the "alias" directive, like so! | ||
21 | |||
22 | {{code language="nginx"}} | ||
23 | server { | ||
![]() |
6.1 | 24 | server_name transistor.one; |
![]() |
1.1 | 25 | |
26 | location /robots.txt { | ||
27 | alias /var/www/static/robots.txt; | ||
28 | } | ||
29 | |||
30 | location /sitemap.xml { | ||
31 | alias /var/www/static/sitemap.xml; | ||
32 | } | ||
33 | |||
34 | location /bin/ { | ||
35 | alias /var/www/static/; | ||
36 | } | ||
37 | |||
38 | location / { | ||
![]() |
6.1 | 39 | return 301 https://alexandru.transistor.one$request_uri; |
![]() |
1.1 | 40 | } |
41 | |||
42 | listen 80; | ||
43 | listen [::]:80; | ||
44 | listen 443 ssl; | ||
45 | listen [::]:443; | ||
46 | } | ||
47 | {{/code}} | ||
48 | |||
49 | Please note that the most important part of that snippet is the **location /bin/** directive, followed by the **alias**, which tells nginx to map all GET requests sent to the **/bin/** URL to the **/var/www/static** directory. | ||
50 | |||
51 | The other directories above it (i.e. the **location /robots.txt** directive) also use the **alias** directive, but they use it to map to a specific file location, rather than a directory location. This is also possible. | ||
![]() |
3.1 | 52 | |
53 | = Change maximum upload size that NGinx accepts = | ||
54 | |||
55 | Sometimes, you may wish to upload files to your server. And sometimes, those files may be quite large in size. | ||
56 | |||
57 | Nginx only allows uploads up to 1MB by default, which is very limited. | ||
58 | |||
59 | At least for certain servers, you may wish to increase this limit to a higher cap. | ||
60 | |||
61 | To do this, you must use the **client_max_body_size** directive, like so: | ||
62 | |||
63 | {{code language="nginx"}} | ||
64 | server { | ||
![]() |
6.1 | 65 | server_name wiki.transistor.one; |
![]() |
3.1 | 66 | |
67 | listen [::]:443 ssl http2; # managed by Certbot | ||
68 | listen 443 ssl http2; # managed by Certbot | ||
69 | |||
70 | client_max_body_size 2500M; | ||
71 | |||
72 | include /etc/nginx/snippets/ssl.conf; | ||
73 | |||
74 | location / { | ||
75 | proxy_pass http://localhost:8081; | ||
76 | } | ||
77 | } | ||
78 | {{/code}} | ||
79 | |||
![]() |
6.1 | 80 | Here, we set the maximum upload file size to 2.5GB (i.e. 2500 MB). Any upload size greater than this will be rejected for the wiki.transistor.one domain. |
![]() |
1.1 | 81 | ))) |
82 | |||
83 | |||
84 | (% class="col-xs-12 col-sm-4" %) | ||
85 | ((( | ||
86 | {{box title="**Contents**"}}{{toc /}}{{/box}} | ||
87 | |||
88 | ))) | ||
89 | ))) |