Linux 中间件Nginx
Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。以下是一些常见的Linux中间件Nginx的使用场景和示例代码:
- 作为静态内容的Web服务器:
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
- 作为代理服务器:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://upstream_server;
}
}
- 配置负载均衡:
upstream backend {
server backend1.example.com;
server backend2.example.com;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://backend;
}
}
- 配置SSL/TLS:
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
- 配置缓存:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://backend;
proxy_cache my_cache;
proxy_cache_valid 200 1d;
}
}
- 配置负载压力测试:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://backend;
proxy_connect_timeout 1s;
proxy_read_timeout 1s;
}
}
- 配置日志记录:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://backend;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
}
}
这些示例仅展示了Nginx配置的一部分,实际配置需要根据具体需求进行调整。
评论已关闭