Nginx随笔
Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。以下是一些Nginx的基本用法和配置示例:
- 安装Nginx
# 使用apt-get安装Nginx
sudo apt-get update
sudo apt-get install nginx
- 启动Nginx
sudo systemctl start nginx
- 设置Nginx开机自启
sudo systemctl enable nginx
- 配置Nginx服务器
编辑Nginx配置文件(通常位于/etc/nginx/nginx.conf
或/etc/nginx/sites-available/default
),例如:
server {
listen 80;
server_name example.com;
location / {
root /var/www/html;
index index.html index.htm;
}
location /images/ {
root /var/www/html;
expires 30d;
}
}
- 重启Nginx以应用更改
sudo systemctl restart 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;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
- 配置负载均衡
upstream backend {
server backend1.example.com;
server backend2.example.com;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
这些只是Nginx配置的基础,Nginx还有很多高级功能和配置选项,如SSL/TLS配置、动态 upstream 管理、缓存、负载均衡算法、HTTP/2支持等。
评论已关闭