1
0
Fork 0
digitalstudium.com/content/en/nginx-lifehacks/nginx-configs-examples.md

88 lines
1.8 KiB
Markdown
Raw Normal View History

2023-03-26 14:41:19 +00:00
---
title: "Nginx: Sample Configuration Files"
2023-04-23 14:56:27 +00:00
category: nginx-lifehacks
filename: nginx-configs-examples
date: 2023-03-26
2023-03-26 14:41:19 +00:00
---
2023-05-19 09:47:46 +00:00
Sometimes there is a need to quickly create an nginx configuration file: for hosting a static website, for proxying requests to upstream server, etc.
2023-03-26 14:41:19 +00:00
This article provides simple examples of Nginx configs for different purposes.
2023-04-23 15:33:17 +00:00
2023-03-26 14:41:19 +00:00
<!--more-->
2023-04-23 15:33:17 +00:00
2023-03-26 14:41:19 +00:00
For the following configs to work, you need to put them in the path `/etc/nginx/conf.d/<some_name>.conf`, then check
config validity:
2023-04-23 15:33:17 +00:00
2023-03-26 14:41:19 +00:00
```bash
nginx -t
```
2023-04-23 15:33:17 +00:00
2023-03-26 14:41:19 +00:00
Then run the command to apply the config:
2023-04-23 15:33:17 +00:00
2023-03-26 14:41:19 +00:00
```bash
sudo service nginx reload
```
2023-04-23 15:33:17 +00:00
2023-03-26 14:41:19 +00:00
## Example 1: hosting a static site.
2023-04-23 15:33:17 +00:00
2023-04-23 14:56:27 +00:00
```nginx
2023-03-26 14:41:19 +00:00
server {
listen 80;
server_name example.com www.example.com;
2023-05-12 12:00:52 +00:00
access_log /var/log/nginx/example.com.log;
2023-03-26 14:41:19 +00:00
location / {
root /var/www/example.com;
index index.html;
}
}
```
2023-04-23 15:33:17 +00:00
2023-03-26 14:41:19 +00:00
## Example 2: proxy server
2023-04-23 15:33:17 +00:00
2023-04-23 14:56:27 +00:00
```nginx
2023-03-26 14:41:19 +00:00
server {
listen 80;
server_name example.com www.example.com;
2023-05-19 09:47:46 +00:00
access_log /var/log/nginx/example.com.log;
2023-03-26 14:41:19 +00:00
location / {
proxy_pass http://127.0.0.1:5000;
proxy_buffering off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
}
}
```
2023-04-23 15:33:17 +00:00
2023-03-26 14:41:19 +00:00
## Example 3: caching proxy
2023-04-23 15:33:17 +00:00
2023-03-26 14:41:19 +00:00
In `/etc/nginx/nginx.conf` add:
2023-04-23 15:33:17 +00:00
2023-04-23 14:56:27 +00:00
```nginx
2023-03-26 14:41:19 +00:00
http {
2023-04-23 15:33:17 +00:00
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=STATIC:10m;
2023-03-26 14:41:19 +00:00
inactive=24h max_size=1g;
}
```
2023-04-23 15:33:17 +00:00
2023-03-26 14:41:19 +00:00
In `/etc/nginx/conf.d` put the following config:
2023-04-23 15:33:17 +00:00
2023-04-23 14:56:27 +00:00
```nginx
2023-03-26 14:41:19 +00:00
server {
listen 80;
server_name example.com www.example.com;
2023-05-19 09:47:46 +00:00
access_log /var/log/nginx/example.com.log;
2023-03-26 14:41:19 +00:00
location / {
proxy_pass http://1.2.3.4;
proxy_set_header Host $host;
proxy_cache STATIC;
proxy_cache_valid 200 1d;
2023-04-23 15:33:17 +00:00
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
2023-03-26 14:41:19 +00:00
}
}
```