server-configs-nginx/README.md

199 lines
4.7 KiB
Markdown
Raw Permalink Normal View History

2013-07-29 01:04:02 +02:00
# [Nginx Server Configs](https://github.com/h5bp/server-configs-nginx)
2023-07-23 12:09:40 +02:00
[![Server CI](https://github.com/h5bp/server-configs-nginx/actions/workflows/server.yml/badge.svg)](https://github.com/h5bp/server-configs-nginx/actions/workflows/server.yml)
2019-02-10 22:15:33 +01:00
2023-05-25 21:29:27 +02:00
**Nginx Server Configs** is a collection of configuration files that can help
2020-04-13 15:00:44 +02:00
your server improve the website's performance and security, while also
2013-11-22 11:32:55 +01:00
ensuring that resources are served with the correct content-type and are
accessible, if needed, even cross-domain.
2015-03-25 11:04:11 +01:00
## Getting Started
2013-11-22 11:32:55 +01:00
2019-02-12 12:25:30 +01:00
Using the Nginx server configs repo directly has a few required steps to be able to work.
* [Nginx Beginners Guide](https://nginx.org/en/docs/beginners_guide.html)
* [Nginx Request Processing](https://nginx.org/en/docs/http/request_processing.html)
2019-02-12 12:25:30 +01:00
### Check `nginx.conf` settings
The first thing to check is that the `nginx.conf` file contains appropriate values for
2020-04-13 21:42:44 +02:00
your specific install.
2019-02-12 12:25:30 +01:00
Most specific variables are:
2020-04-14 10:20:46 +02:00
2019-02-12 12:25:30 +01:00
* `user`
* `error_log`
* `pid`
* `access_log`
### Nginx test and restart
* To verify Nginx config
2020-04-14 10:20:46 +02:00
2019-02-12 12:25:30 +01:00
```shell
2020-04-14 10:20:46 +02:00
nginx -t
2019-02-12 12:25:30 +01:00
```
* To verify Nginx config with a custom file
2020-04-14 10:20:46 +02:00
2019-02-12 12:25:30 +01:00
```shell
2020-04-14 10:20:46 +02:00
nginx -t -c nginx.conf
2019-02-12 12:25:30 +01:00
```
2020-04-14 11:53:56 +02:00
* To reload Nginx and apply the new config
2020-04-14 10:20:46 +02:00
2019-02-12 12:25:30 +01:00
```shell
2020-04-14 10:20:46 +02:00
nginx -s reload
2019-02-12 12:25:30 +01:00
```
2023-05-25 21:29:27 +02:00
### Repository structure
This repository has the following structure:
2020-04-14 10:20:46 +02:00
```text
./
├── conf.d/
│ ├── default.conf
2020-04-14 10:20:46 +02:00
│ └── templates/
├── h5bp/
│ ├── basic.conf
│ ├── location/
│ └── .../
├── custom.d/
│ └── .../
├── mime.types
└── nginx.conf
```
* **`conf.d/`**
2020-04-13 21:42:44 +02:00
This directory should contain all the `server` definitions.
Except if they are dot prefixed or non `.conf` extension, all files in this
2023-05-25 21:29:27 +02:00
directory are loaded automatically.
* **`templates` folder**
2023-05-25 21:29:27 +02:00
Files in this directory contain a `server` template for secure and non-secure
hosts. They are intended to be copied in the `conf.d` directory with all
`example.com` occurrences changed to the target host.
* **`h5bp/`**
This directory contains config snippets (mixins) to be included as desired.
2020-04-13 21:42:44 +02:00
2023-05-25 21:29:27 +02:00
There are two types of config files provided: individual config snippets and
combined config files which provide convenient defaults.
* **`basic.conf`**
2020-04-13 21:42:44 +02:00
This file loads a small subset of the rules provided by this repository to add
2023-05-25 21:29:27 +02:00
`expires` headers, allow cross-domain fonts and protect system files from web
access.
2019-02-12 12:25:30 +01:00
The `basic.conf` file includes the rules which are recommended to always be
defined.
* **`location/`**
2023-05-25 21:29:27 +02:00
Files in this directory contain one or more `location` directives. They are
intended to be loaded in the `server` context (or, in a nested `location` block).
* **`custom.d/`**
This directory should contain all the custom `nginx.conf` configuration.
Except if they are dot prefixed or non `.conf` extension, all files in this
2023-05-25 21:29:27 +02:00
folder are loaded automatically.
* **`mime.types`**
2023-05-25 21:29:27 +02:00
The `mime.types` file is responsible for mapping file extensions to MIME types.
* **`nginx.conf`**
The main Nginx config file.
## Usage
### As a reference
To use as reference requires no special installation steps, download/checkout the
repository to a convenient location and adapt your existing Nginx configuration
incorporating the desired functionality from this repository.
2020-04-13 21:42:44 +02:00
Download the [latest release archive](https://github.com/h5bp/server-configs-nginx/releases/latest).
### Directly
2020-04-13 21:42:44 +02:00
To use directly, replace the Nginx config directory with this repository.
For example:
```shell
nginx -s stop
cd /etc
mv nginx nginx-previous
git clone https://github.com/h5bp/server-configs-nginx.git nginx
# install-specific edits
nginx
```
### Manage sites
```bash
2020-04-14 10:20:46 +02:00
cd /etc/nginx/conf.d
```
* Creating a new site
2020-04-14 10:20:46 +02:00
```bash
2020-04-14 10:20:46 +02:00
cp templates/example.com.conf .actual-hostname.conf
sed -i 's/example.com/actual-hostname/g' .actual-hostname.conf
```
* Enabling a site
2020-04-14 10:20:46 +02:00
```bash
2020-04-14 10:20:46 +02:00
mv .actual-hostname.conf actual-hostname.conf
```
2020-04-13 15:00:44 +02:00
* Disabling a site
2020-04-14 10:20:46 +02:00
```bash
2020-04-14 10:20:46 +02:00
mv actual-hostname.conf .actual-hostname.conf
```
```bash
2020-04-14 10:20:46 +02:00
nginx -s reload
```
## Support
2019-02-10 20:33:30 +01:00
* Nginx v**1.8.0**+
2013-11-22 11:32:55 +01:00
2015-03-25 11:04:11 +01:00
2018-11-23 13:28:12 +01:00
## Contributing
2013-07-29 01:04:02 +02:00
2018-11-23 13:28:12 +01:00
Anyone is welcome to [contribute](.github/CONTRIBUTING.md),
however, if you decide to get involved, please take a moment to review
the [guidelines](.github/CONTRIBUTING.md):
2013-07-29 01:04:02 +02:00
2018-11-23 13:28:12 +01:00
* [Bug reports](.github/CONTRIBUTING.md#bugs)
* [Feature requests](.github/CONTRIBUTING.md#features)
* [Pull requests](.github/CONTRIBUTING.md#pull-requests)
2013-07-29 01:04:02 +02:00
2015-03-25 11:04:11 +01:00
2013-07-29 01:04:02 +02:00
## Acknowledgements
2020-04-13 15:00:44 +02:00
[Nginx Server Configs](https://github.com/h5bp/server-configs-nginx) is
only possible thanks to all the awesome
2013-07-29 01:04:02 +02:00
[contributors](https://github.com/h5bp/server-configs-nginx/graphs/contributors)!
## License
2015-03-25 11:04:11 +01:00
The code is available under the [MIT license](LICENSE.txt).