sudo apt update
sudo apt install nginx
sudo yum install epel-release
sudo yum install nginx
sudo systemctl start nginx
NGINX的主要配置文件:/etc/nginx/nginx.conf
NGINX默认站点配置文件:/etc/nginx/sites-available/default
编辑默认站点配置文件,配置静态文件托管:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html; # 静态文件存放路径
index index.html index.htm;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}
sudo systemctl restart nginx
配置NGINX作为反向代理服务器,将请求转发至后端应用:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend_server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
# 其他反向代理配置
}
}
实现负载均衡配置:
upstream backend {
server backend1.example.com;
server backend2.example.com;
# 添加更多后端服务器
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend;
# 负载均衡配置
}
}
为站点启用 HTTPS:
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /path/to/certificate.crt;
ssl_certificate_key /path/to/private.key;
location / {
# HTTPS 配置
}
}