实例一, Nginx 虚拟主机配置
拷贝一份配置文件, 配置虚拟主机; 并且创建一个 nginx 的普通用户
- [root@CentOS-75 ~]# cd /usr/local/nginx/conf
- [root@CentOS-75 conf]# cp nginx.conf.default nginx.conf
- [root@CentOS-75 conf]# useradd nginx -s /sbin/nologin
配置虚拟主机: www.gz1.com 以及配置文件详解
[root@CentOS-75 ~]# VIM nginx.conf
1) 全局变量 部分:-----------------------------------------------------------
- user nginx nginx; ## 用户和用户组都为 nginx
- worker_processes 1; ##Nginx 开启的进程数, 建议设置为等于 CPU 总核心数
- error_log /usr/local/nginx/logs/error.log info; ## 定义全局错误日志定义类型
- pid /usr/local/nginx/logs/nginx.pid; ## 指定进程 ID 存储文件位置
2) 事件驱动模型配置 部分:-------------------------------------------------------
- events {
- worker_connections 1024; ## 每个进程可以处理的最大连接数
- #multi_accept on; ## 一定程度降低负载, 但服务器吞吐量大时
- }
3) 设定 http 的部分:-----------------------------------------------------------
- http {
- include mime.types; ## 文件扩展名与文件类型映射表, 设定 mime 类型, 类型由 mime.type 文件定义
- default_type application/octet-stream; ## 默认文件类型
- charset utf-8; ## 服务器默认编码
- #include /etc/nginx/proxy.conf; ## 反向代理配置
- #include /etc/nginx/fastcgi.conf; ##fastcgi 配置
- 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; ## 是否调用 sendfile 函数来输出文件 (对于普通应用)
- tcp_nopush off; ## 禁止了小包的发送
- tcp_nodelay on; ## 允许小包的发送
- #keepalive_timeout 0;
- keepalive_timeout 10; ## 连接超时时间, 单位是秒
- #gzip 模块设置 : ------------------------------------------------------
- #gzip on; ## 开启 gzip 压缩输出
- #gzip_min_length 1k; ## 最小压缩文件大小
- #gzip_buffers 4 16k; ## 压缩缓冲区
- #gzip_http_version 1.0; ## 压缩版本 (默认 1.1, 前端如果是 squid2.5 请使用 1.0)
- #gzip_comp_level 2; ## 压缩等级
- #gzip_types text/plain application/x-JavaScript text/CSS application/xml;
- ## 压缩类型, 默认就已经包含 text/html
- # 设定负载均衡的服务器列表 :(nginx 作为 web 代理角色 )--------------------
- #upstream tomcat { ##tomcat 命名
- #server 127.0.0.1:8080 weight=1; ## weight 权值越高被分配到的几率越大
- #server 192.168.1.116:8081 weight=1;
- #}
- #upstream tomcat2 { ## 可以配置多个负载均衡的服务器列表
- #server 127.0.0.1:8080 weight=1;
- #server 192.168.1.116:8081 weight=1;
- #}
- # 虚拟主机的配置 :-------------------------------------------------------
- server {
- listen 80; ## 监听端口
- server_name www.gz1.com,www.gz2.com; ## 域名可以有多个, 用空格隔开
- #charset utf-8; ## 字符编码 (上面写有下面可以不用写)
- access_log /data/Web/gz1.com/logs/gz1.access.log main; ## 定义本虚拟主机的访问日志
- #autoindex on; ## 所有的文件都以目录形式访问
- location / {
- root /data/Web/gz1.com; ## 访问路径
- index index.HTML index.htm; ## 定义索引首页
- proxy_intercept_errors on; ## 自定义 404 界面
- }
- error_page 404 /404.HTML; ## 在根须根下寻找 404.HTML
- error_page 500 502 503 504 /50x.HTML;
- location = /404.HTML { ## 错误页面都在此路径下寻找
- root /data/Web/gz1.com;
- }
- }
- }
在 客户端 C:\Windows\System32\drivers\etc 上添加解析
- 192.168.10.75 www.gz1.com
- 192.168.10.75 www.gz2.com
在 /data/Web/gz1.com 下创建对应的首页文件以及相应的目录
- [root@CentOS-75 ~]# mkdir -p /data/Web/gz1.com/logs
- [root@CentOS-75 gz1.com]# ls
- 404.HTML index.HTML logs
实例二, Nginx 访问控制
1, 基于用户的访问控制:
1), 创建用户, 并且认证加密. 此创建的用户并非 Linux 的登陆用户
- [root@CentOS-75 conf]# yum install -y httpd-tools ## 安装插件
- [root@CentOS-75 conf]# htpasswd -cm /usr/local/nginx/conf/passwd user1
- ## "c" 表示是第一次创建 "m" 表示 MD5 加密
- New password:
- Re-type new password:
- Adding password for user user1
- [root@CentOS-75 conf]# htpasswd -m /usr/local/nginx/conf/passwd user2
- ## 后续创建不需要加 "c"
- New password:
- Re-type new password:
- Adding password for user user2
- [root@CentOS-75 conf]# cat passwd
- user1:$apr1$ZBflnq4Y$czYhTd0xG4JoXHvNaoY9B.
- user2:$apr1$WD8DoNj2$oQikEyEP3gweTAUKJyb3C0
2), 虚拟主机添加的配置段
- [root@CentOS-75 ~]# VIM /usr/local/nginx/conf/nginx.conf
- ......
- location /logs {
- root /data/Web/gz1.com; #### 访问路径
- auth_basic "nginx"; ## 基础认证 命名 nginx(可自定义)
- auth_basic_user_file /usr/local/nginx/conf/passwd; ## 密码认证路径
- autoindex on; ## 显示的文件以树状陈列
- }
- ......
3), 访问 www.gz1.com/logs 登陆认证
4), 登陆成功, 访问
2, 基于主机的访问控制:
1), 在 /data/Web/gz1.com 下创建一个 test.txt 文件测试
[root@CentOS-75 gz1.com]# VIM test.txt
只允许 192.168.10.1 访问
拒绝其他 ip 访问
2), 虚拟主机添加的配置段
- [root@CentOS-75 ~]# VIM /usr/local/nginx/conf/nginx.conf
- ......
- location ~*/.*\.txt$ {
- root /data/Web/gz1.com;
- allow 192.168.10.1;
- deny all;
- }
- ......
3), 访问 www.gz1.com/test.txt
用不是 192.168.10.1 的其他主机访问; 显示 403 错误, 没有权限访问
实例三, Nginx 地址重写 rewrite:
地址重写
1), 地址从写访问 /static 下所有文件都会跳转到 /write/2.HTML (创建对应的目录和文件)
- [root@CentOS-75 gz1.com]# ls
- 404.HTML index.HTML logs static test.txt write
- [root@CentOS-75 gz1.com]# ls write/ static/
- static/:
- 1.HTML
- write/:
- 2.HTML
2), 网页重写虚拟主机添加的配置段
- [root@CentOS-75 ~]# VIM /usr/local/nginx/conf/nginx.conf
- ......
- location /static {
- root /data/Web/gz1.com;
- rewrite .*/write/2.HTML permanent;
- }
- ......
3), 访问 http://www.gz1.com/static 会自动跳转到 http://www.gz1.com/write/2.html
域名重写, 引用 if 判断
访问 www.gz2.com 的定向到 www.baidu.com; 域名重写虚拟主机添加的配置段
- [root@CentOS-75 ~]# VIM /usr/local/nginx/conf/nginx.conf
- ......
- server {
- location / {
- root /data/Web/gz1.com;
- index index.HTML index.htm;
- proxy_intercept_errors on;
- if ( $host ~* www.gz2.com ) {
- rewrite .* http://www.baidu.com permanent;
- }
- }
- }
- ......
实例四, Nginx 反向代理 :
1, 通用匹配 代理
- ......
- location /tset1 {
- root /data/Web/gz1.com;
- proxy_pass http://www.jd.com;
- }
- ......
2, 正则表达式 匹配代理
- ......
- location ~ ^/test2 {
- root /data/Web/gz1.com;
- proxy_pass http://www.taobao.com;
- }
- ......
3, 地址重写 代理
- ......
- location / {
- root /data/Web/gz1.com;
- rewrite /(.*)$ /index.PHP?page=$1 break;
- proxy_pass http://www.tmall.com;
- }
- ......
4,proxy 模块的指令
- proxy_pass: ## 指定将请求代理至 upstream server 的 URL 路径;
- proxy_set_header: ## 发送至 upsream server 的报文的某首部进行重写;
- proxy_intercept_errors on; ## 拦截后端服务器大于 300 的错误信息 (自定义 404 页面)
在根系下配置 proxy_intercept_errors on; 自定义 404 页面
- ......
- server {
- location / {
- root /data/Web/gz1.com;
- index index.HTML index.htm;
- proxy_intercept_errors on;
- }
- error_page 404 /404.HTML;
- location = /404.HTML {
- root /data/Web/gz1.com;
- }
- }
- ......
实例五, Gzip 传输压缩功能:
在 http 内 (server 虚拟主机配置段外):--------------------------------
- ......
- http{
- gzip on; ## 开启 Gzip
- gzip_min_length 1k; ## 大于 1K 的才压缩, 一般不用改
- gzip_buffers 4 16k; ## 用于压缩的缓冲区大小
- gzip_comp_level 2; ## 压缩级别, 1-10, 数字越大压缩的越好, 时间也越长
- gzip_types text/plain application/x-JavaScript text/CSS application/xml text/JavaScript application/x-httpd-PHP ;
- }
- ......
实例六, 负载均衡:
添加 upstream 模块
- ......
- http{
- upstream nginx {
- server 192.168.10.74 weight=1 max_fails=10 fail_timeout=10s;
- server 192.168.10.75 weight=1 max_fails=10 fail_timeout=10s;
- }
- }
- server {
- location / {
- proxy_pass http://nginx;
- proxy_set_header X-Real-IP $remote_addr;
- }
- }
- }
- ......
在 客户端 C:\Windows\System32\drivers\etc 上添加解析
192.168.10.73 www.gz1.com
访问 www.gz1.com 不断刷新观察效果
实例七, Nginx 的缓存模块功能:
- ......
- http {
- proxy_cache_path /nginx/cache/first levels=1:2 keys_zone=first:20m max_size=1g;
- server {
- ......
- location / {
- ......
- proxy_cache first;
- proxy_cache_valid 200 10m;
- }
- }
- }
- ......
- [root@CentOS-75 ~]# mkdir -p /nginx/cache/first
在 http{ } 段 (server 段前), 添加以下语句, 在服务器响应报文中, 添加一个首部
add_header X-cache "$upstream_cache_status from $server_addr" ;
删除缓存路径下的内容, 再次观察.
[root@CentOS-75 ~]# rm -rf /nginx/cache/first/*
实例八, Nginx 的限速模块 以及 其他模块:
1, 限速模块
- ......
- http{
- limit_conn_zone $binary_remote_addr zone=one:10m;
- server {
- location /download {
- limit_conn one 1; ## 限制在 one 中记录状态的每个 IP 只能发起一个并发连接
- limit_rate_after 1000k; ## 在下载 1000k 后开始限速
- limit_rate 100k; ## 对每个连接限速 100k
- }
- }
- }
- ......
2, 限制单个 IP 最大连接数 (线程数)
- http {
- limit_conn_zone $binary_remote_addr zone=client_addr:10m;
- server {
- location /download {
- limit_conn one 1;
- limit_rate_after 1000k;
- limit_rate 100k;
- limit_conn client_addr 10; ## 发数设置为 10
- }
- }
- }
3, 隐藏 Nginx 版本信息
版本信息关闭前
在配置文件上加
- http{
- server_tokens off;
- }
版本信息关闭后
来源: http://www.bubuko.com/infodetail-2897396.html