nginx入门
什么是nginx
nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。由俄罗斯的程序设计师Igor Sysoev所开发,官方测试nginx能够支支撑5万并发链接,并且cpu、内存等资源消耗却非常低,运行非常稳定,所以现在很多知名的公司都在使用nginx。
应用场景
- http服务器
- 虚拟主机
- 反向代理
配置(细节见注解)
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
limit_req_zone $binary_remote_addr zone=one:10m rate=30r/m;
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# another virtual host using mix of IP-, name-, and port-based configuration
# 负载均衡配置
# 轮询配置
#upstream backserver {
#server 127.0.0.1:8080 ;
#server 127.0.0.1:8081 ;
#}
# 根据权重配置
#upstream backserver {
#server 127.0.0.1:8080 weight=1;
#server 127.0.0.1:8081 weight=3;
#}
# 根据ip hash
#upstream backserver {
#ip_hash;
#server 192.168.0.1:80;
#server 192.168.0.2:80;
#}
# nginx 防盗链配置
#location ~ .*\.(jpg|jpeg|JPG|png|gif|icon)$ {
# valid_referers blocked http://www.lidaxia.top;
# if ($invalid_referer) {
# return 403;
# }
# }
# 防止ddos(Distributed Denial of Service 分布式拒绝服务) 攻击 (限制请求速度)
limit_req_zone $binary_remote_addr zone=one:10m rate=30r/m;
server {
proxy_pass http://127.0.0.1:8080;
index index.html index.htm;
location /login.html {
limit_req zone=one;
}
}
server {
listen 80;
server_name www.lidaxia.top;
location / {
proxy_pass http://127.0.0.1:8080;
index index.html index.htm;
# 宕机轮询配置
#proxy_connect_timeout 1;
#proxy_send_timeout 1;
#proxy_read_timeout 1;
}
}
### 解决跨域
#server {
# listen 80;
# server_name www.lidaxia.top;
# location /A {
# proxy_pass http://a.a.top:81/A;
# index index.html index.htm;
# }
# location /B {
# proxy_pass http://b.b.top:81/B;
# index index.html index.htm;
# }
#}
server {
listen 80;
server_name b8081.lidaxia.top;
location / {
proxy_pass http://127.0.0.1:8081;
index index.html index.htm;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}