Linux-nginx(安装配置nginx、配置反向代理、Nginx配置负载均衡、动静分离)
以下是一个基于Docker的简化版本的Nginx配置实例,包括安装配置Nginx、配置反向代理和Nginx配置负载均衡,以及动静分离。
- 安装Nginx:
FROM nginx:alpine
COPY nginx.conf /etc/nginx/nginx.conf
- 配置反向代理:
http {
server {
listen 80;
server_name myapp.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;
}
}
}
- 配置负载均衡:
upstream backend {
server backend1.example.com;
server backend2.example.com;
}
- 动静分离:
server {
listen 80;
server_name myapp.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;
}
location ~* \.(jpg|jpeg|png|css|js|gif|ico)$ {
root /path/to/static/files;
expires 30d;
}
}
这些配置可以根据实际需求进行调整和扩展。在实际部署时,你需要将Nginx配置文件复制到容器中的相应位置,并确保后端服务器(backend1.example.com 和 backend2.example.com)可以被Nginx代理。
评论已关闭