Installing a web server/Nginx web server
Installing Nginx web server
[edit | edit source]w:Nginx web server that was written in 2004 with an explicit goal of outperforming the traditional and widely used Apache web server. Official documentation is on https://nginx.org/en/docs/.
- Configuration File:
/etc/nginx/nginx.conf
- Additional Configuration file:
/etc/nginx/conf.d/default.conf
- Error log file:
/var/log/nginx/error.log
Nginx vs Apache
[edit | edit source]Apache was first released in 1995 while Nginx was first released 9 years later in 2004, so traditionally there has been a more broad knowledge of Apache web server than Nginx. We will present main differences from Apache to Nginx for newcomers. Both servers support virtual host configuration and include directives.
Configuration differences
[edit | edit source]- No concept equivalent to
.htaccess
files - Configuration file based on directives:
nginx.conf
[1] vshttpd.conf
, arguably Nginx configuration language is easier to read.
Installing and Operating Nginx web server on CentOS Linux
[edit | edit source]Installation packages for different Linux platforms is are available on Nginx linux package webpage[2]. Follow this three easy steps on CentOS Linux:
- Update your package repository using http://nginx.org/en/linux_packages.html information
- run yum update
- sudo yum install -y nginx
Running Nginx web server
[edit | edit source]- Starting Nginx:
sudo systemctl start nginx
sudo systemctl enable nginx
Check Nginx running
[edit | edit source]- Check default webpage/virtualhost:
curl http:://localhost
- In case of error check error log file:
/var/log/nginx/error.log
- In case of error check error log file:
- Check connection to example.com virtualhost:
curl --header "Host: example.com" localhost
Configuring Nginx
[edit | edit source]Configuration of Nginx on CentOS is done in /etc/nginx/nginx.conf (upstream github file[3])
Basic Nginx Operation
[edit | edit source]- Check status:
systemctl reload nginx
(not output expected if everything runs properly) - Starting Nginx:
systemctl start nginx
ornginx -s [stop|quit|reload|reopen]
[4] - Stopping Nginx:
systemctl stop nginx
- Reload configuration after every configuration change:
systemctl reload nginx
, Nginx has to be reload every time we want any configuration change take effect. - Check Configuration:
nginx -t
Nginx Configuration
[edit | edit source]Basic Nginx Configuration
[edit | edit source]Example of the simplest possible server configuration file, listening on 80 port (default http port)
/etc/nginx/conf.d/default.conf server { listen 80; root /usr/share/nginx/html; }
Example of the virtual host for www.example.com or example.com domain
/etc/nginx/conf.d/default.conf server { listen 80; server_name example.com www.example.com root /var/www/example.com/html; }
Checking Nginx errors
[edit | edit source]Errors are write to error log file /var/log/nginx/error.log
, you can use grep to search for errors. For example:
grep error /var/log/nginx/error.log 2011/03/08 11:12:21 [error] 1755#1754 *8 "/var/www/example.com/html/index.html" is forbidden (13: Permission denied), client: 1.123.51.252, server: http://Example/URL.com, request: "GET / HTTP/1.1", host: "http://Example/URL.com"
or
grep emerg /var/log/nginx/error.log
Permission denied errors can be cause by Selinux security module misconfiguration[5], adding httpd_sys_content_t context can solve permission denied errors.
semanage fcontext -a -t httpd_sys_content_t "/var/www(/.*)?" semanage fcontext -l | grep /var/www restorecon -R -v /var/www
Next Steps
[edit | edit source]Now you can check official documentation and learn how to
- Modify default error pages using error_page directive
- Use Access Control with HTTP Basic Auth using
location
[6] directive ,http_auth_basic
module[7],htpasswd
utility and.htpasswd
files - Use w:Digest access authentication
- Configure w:https using self-signed certificates using openssl (for example:
openssl req -x590 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/private.key -out /etc/nginx/ssl/public.pem
) - Monitoring Nginx web server
Additional Nginx documentation
[edit | edit source]- Nginx directives: http://nginx.org/en/docs/dirindex.html. Common Nginx directives are:
server[8], server_name, listen[9], root[10]
Activities
[edit | edit source]Basic
[edit | edit source]- Read Nginx release Notes / Changelog
- Install Nginx using docker
Advanced
[edit | edit source]- Configure Nginx as reverse proxy with Websocket support
See also
[edit | edit source]- wikipedia:Nginx
- w:URL_redirection#nginx_rewrite
- https://en.wikipedia.org/wiki/Category:Free_web_server_software
References
[edit | edit source]- ↑ https://www.digitalocean.com/community/tutorials/understanding-the-nginx-configuration-file-structure-and-configuration-contexts
- ↑ http://nginx.org/en/linux_packages.html
- ↑ https://github.com/nginx/nginx/blob/master/conf/nginx.conf
- ↑ https://nginx.org/en/docs/beginners_guide.html#control
- ↑ https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/selinux_users_and_administrators_guide/sect-security-enhanced_linux-working_with_selinux-selinux_contexts_labeling_files
- ↑ https://nginx.org/en/docs/http/ngx_http_core_module.html#location
- ↑ https://nginx.org/en/docs/http/ngx_http_auth_basic_module.html
- ↑ http://nginx.org/en/docs/http/ngx_http_core_module.html#server
- ↑ http://nginx.org/en/docs/http/ngx_http_core_module.html#listen
- ↑ http://nginx.org/en/docs/http/ngx_http_core_module.html#root