大家好,我是小麦。今天分享一篇关于NGINX常用的配置清单。
作为一名服务端研发工程师,接触服务器的时间也比较多。在项目对外提供服务,我们一般会使用NGINX来提供对外的服务,因此NGINX的操作也非常多,这就需要我们对NGINX比较熟悉,因此特意整理了一份相对完善的清单。
首先还是对NGINX做一个简单的介绍,分别从是什么?有什么优势?以及使用的业务场景来聊聊NGINX。
NGINX是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3代理服务器。它最初由Igor Sysoev开发,旨在解决C10K问题(即在单台服务器上同时处理1万个客户端连接)。如今,NGINX已经发展成为广泛使用的Web服务器、负载均衡器和反向代理服务器。
1、Web服务器:
作为HTTP和HTTPS服务器,提供静态和动态内容服务。用于托管网站和Web应用程序。
2、反向代理:
代理客户端请求,将请求转发到一个或多个后端服务器。用于隐藏后端服务器的真实地址,提供负载均衡和缓存功能。
3、负载均衡器:将流量分发到多个后端服务器,以提高应用程序的可用性和响应速度。支持多种负载均衡策略(如轮询、IP哈希、最少连接等)。
4、内容缓存:缓存后端服务器的响应,减少后端服务器的负载,提高响应速度。适用于静态内容和动态内容的缓存。
5、 SSL/TLS终端:处理SSL/TLS加密和解密,保护数据传输的安全性。用于HTTPS网站和Web应用程序的安全通信。
6、API网关:管理和保护API请求,提供身份验证、速率限制、日志记录和监控等功能。适用于微服务架构和分布式系统。
7、 邮件代理服务器: 代理IMAP和POP3邮件请求,提高邮件服务的性能和安全性。
server {
listen 80;
server_name example.com;
location / {
root /var/www/html;
index index.html index.htm;
}
}
server {
listen 80;
server_name api.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;
}
}
http {
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;
}
}
}
server {
listen 80;
server_name cache.example.com;
location / {
proxy_pass http://backend_server;
proxy_cache my_cache;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
}
}
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m inactive=60m;
上述内容便是我对NGINX的大致总结,下面就来分享一下具体的只知识内容。
user www-data;
worker_processes auto;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log warn;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location /api/ {
limit_req zone=mylimit burst=5 nodelay;
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。