nginx 基本介绍
- web服务器
- 代理服务器
- C语言编写
- 开源
为什么选择nginx
- 高性能 高并发
- 高扩展性(功能模块化)
- 高可靠性(持续不断运行)
- nginx应用场景
- nginx web服务
- nginx代理
- nginx反向代理----->负载均衡
- nginx反向代理----->缓存
- nginx静态加速
- nginx动静分离
- nginx 全站https
nginx安装
nginx官方源
vim /etc/yum.repos.d/nginx.repo
```powershell
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
nginx 安装
yum install nginx
nginx配置文件
/etc/nginx/nginx.conf
user nginx; # Nginx进程的运行用户身份
worker_processes 1; # Nginx运行的worker进程数
error_log /var/log/nginx/error.log warn; # Nginx错误日志存放的路径
pid /var/run/nginx.pid; # Nginx进程运行的PID号
events {
worker_connections 1024; #每个worker进程能接受的最大连接数
use epoll;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
#访问日志的路径 访问日志的存储格式是main格式, main格式在log_format中进行定义
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;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf; #包含 *.conf
}
nginx运行一个网站
vim /etc/nginx/conf.d/game.wuxing.com.conf
server {
listen 80; #监听80端口
server_name game.huizi.com; #网站的域名
location / { #匹配网站的uri
root /code; #返回资源的具体路径
index index.html; #返回的具体资源名称
}
}
nginx虚拟主机
- 基于IP (几乎不怎么使用)
- 基于端口 (内部使用)
- 基于域名
cat /etc/nginx/conf.d/game.dahuizi.com.conf
server {
listen 80;
server_name game.dahuizi.com;
location / {
root /code;
index index.html;
}
}
autoindex 目录索引
vim /etc/nginx/conf.d/mirrors.dahuizi.com.conf
server {
listen 80;
server_name mirror.dahuizi.com;
charset utf8;
location / {
root /code;
index index.html;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
autoindex_format html;
}
}
基于IP实现访问控制
ngx_http_access_module
cat /etc/nginx/conf.d/mirror.dahuizi.com.conf
server {
listen 80;
server_name mirror.dahuizi.com;
charset utf8;
root /code;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
autoindex_format html;
location / {
index index.html;
}
location /centos {
allow 10.0.0.1/32;
deny all;
}
location /ubuntu {
deny 10.0.0.100/32;
allow all;
}
}
基于用户名和密码的访问控制
ngx_http_auth_basic_module
[root@web01 ~]# yum install httpd-tools -y
[root@web01 ~]# htpasswd -bc /etc/nginx/auth_conf dahuizi 123
[root@web01 ~]# cat /etc/nginx/conf.d/mirror.dahuizi.com.conf
server {
listen 80;
server_name mirror.dahuizi.com;
charset utf8;
root /code;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
autoindex_format html;
location / {
index index.html;
}
location /centos {
allow 10.0.0.1/32;
deny all;
}
location /ubuntu {
auth_basic "Dahuizi Site";
auth_basic_user_file /etc/nginx/auth_conf;
}
}
limit_conn 连接限制
ngx_http_limit_conn_module
Syntax: limit_conn_zone key zone=name:size;
Default: —
Context: http
Syntax: limit_conn zone number;
Default: —
Context: http, server, location
Syntax: limit_conn_status code;
Default: limit_conn_status 503;
Context: http, server, location
This directive appeared in version 1.3.15.
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
location /download/ {
limit_conn addr 1;
}
limit_req 请求限制
ngx_http_limit_req_module
Syntax: limit_req_zone key zone=name:size rate=rate [sync];
Default: —
Context: http
Syntax: limit_req zone=name [burst=number] [nodelay | delay=number];
Default: —
Context: http, server, location
limit_req_zone $binary_remote_addr zone=perip:10m rate=1r/s;
limit_req_zone $server_name zone=perserver:10m rate=10r/s;
server {
...
limit_req zone=perip burst=5 nodelay;
limit_req zone=perserver burst=10;
}
Syntax: limit_req_status code;
Default: limit_req_status 503;
Context: http, server, location
This directive appeared in version1.3.15.
Syntax: limit_req_log_level info | notice | warn | error;
Default: limit_req_log_level error;
Context: http, server, location
This directive appeared in version 0.8.18.