Installing Dependencies
yum install openssl -y
yum install zlib -y
yum install pcre -y
Add the official Nginx repository:
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
Installing Nginx
yum install nginx -y
Managing Nginx Service
systemctl start nginx # Start Nginx
systemctl restart nginx # Restart Nginx
systemctl status nginx # Check status
systemctl enable nginx # Enable on boot
Verifying Installation
Open port 80 in the server security group, then access http://{server_ip} in a browser. The default welcome page indicates successful installation and startup.
Configuration file locations:
- Main config:
/etc/nginx/nginx.conf - Additional configs:
/etc/nginx/conf.d/(all*.conffiles are automatically included) - Default config:
/etc/nginx/conf.d/default.conf
Basic Cnofiguration Examples
Reverse Proxy
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Load Balancing
upstream backend_pool {
ip_hash;
server 127.0.0.1:8080 weight=3;
server 127.0.0.1:8081 weight=2;
server 127.0.0.1:8082 weight=1;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend_pool;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
After modifying configuration files, test the configuration:
nginx -t
Then reload Nginx to apply changes:
systemctl reload nginx