1.1. 启用虚拟主机配置:
vim /opt/nginx/conf/nginx.conf
删除server 主机配置文件,增长include vhost/*.conf;
1.2. 建立虚拟主机配置存放目录:mkdir /opt/nginx/conf/vhost
1.3. 建立虚拟主机配置:
cd !$; vim default.conf
php
server { listen 80; # 监听的端口 server_name localhost; # 监听的域名 index index.html index.htm index.php; root /opt/www/html; # 网页路径 }
2.1. 安装http
yum install -y httpd
(用来建立用户、密码)
2.2. 建立用户、密码
htpasswd -c /opt/nginx/conf/htpasswd admin
(运行‘htpasswd’建立用户名、密码时,不能在用户名、密码存放的目录下运行该命令;这里不能在‘conf’目录下运行‘htpasswd’)
2.3. 虚拟主机中的配置css
location / # 做用整个网站:“/”,做用某个目录: “/admin/”,做用某个url:"~ admin.php" ~匹配 { auth_basic "Auth"; auth_basic_user_file /opt/nginx/conf/htpasswd; # 定义用户认证的用户名、密码路径; }
2.4. 检查Nginx配置&从新加载Nginx配置
/opt/nginx/sbin/nginx -t
检查Nginx配置;
/opt/nginx/sbin/nginx -s reload
从新加载Nginx;
若以前运行过上述命令,则可按“Ctrl+R”建,直接输入 -t && -s reload
2.5. 验证
curl -x127.0.0.1:80 test.com -I
正常会出现401报错信息,须要指定用户名、密码;
curl -uadmin:admin -x127.0.0.1:80 test.com -I
正常会出现200 的代码,这个说明用户认证已经生效了;html
3.1. 更改虚拟主机配置文件,如:test.com.confnginx
server { listen 80; server_name test.com test1.com test2.com; # 指定域名,这里能够指定多个域名 index index.html index.htm index.php; root /data/wwwroot/test.com; if ($host != 'test.com' ) # if 在这里是用来重定向的,这里是将全部的域名都重定向到 test.com { rewrite ^/(.*)$ http://test.com/$1 permanent; # “rewrite” 跳转,“ ^/(.*)$ ” 以“/”开头任意结尾;permanent 301跳转、redirect 302 跳转; } }
4.1. 主配置文件“nginx.conf”中搜索“log_format”,这个是定义log样式的;默认日志样式以下:web
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' ' $host "$request_uri" $status' ' "$http_referer" "$http_user_agent"';
4.2. 日志参数说明:算法
- combined_realip 日志格式名称,能够自定义;
- $remote_addr 客户端IP(公网IP);
- $http_x_forwarded_for 代理服务器的IP;
- $time_local 服务器本地时间;
- $host 访问主机名(域名);
- $request_uri 访问的url地址;
- $status 状态码 (如:40四、30一、30二、200等);
- $http_referer
- $http_user_agent
4.3. 虚拟主机日志配置
除了在主配置文件“nginx.conf”里定义日志格式外,还须要在虚拟主机配置文件中增长:access_log /tmp/1.log combined_realip;
;
这里的“combined_realip”就是在nginx.conf中定义的日志格式名字;shell
4.4. 检查配置,从新加载Nginx配置
-t && -s reload
curl -x127.0.0.1:80 test.com -I
cat /tmp/1.log
vim
5.1. 自定义shell 脚本,用于日志切割;
vim /usr/local/sbin/nginx_log_rotate.sh
//写入以下内容浏览器
#! /bin/bash ## 假设nginx的日志存放路径为/data/logs/ d=`date -d "-1 day" +%Y%m%d` logdir="/data/logs" nginx_pid="/usr/local/nginx/logs/nginx.pid" cd $logdir for log in `ls *.log` do mv $log $log-$d done /bin/kill -HUP `cat $nginx_pid`
5.2. 任务计划
crontab -e
添加定时任务
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
缓存
6.1. 具体配置以下:
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 7d; # 缓存过时时间 access_log off; # 关闭日志记录 } location ~ .*\.(js|css)$ { expires 12h; access_log off; }
7.1. 配置以下,能够和上面的配置结合起来
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ { expires 7d; # 缓存过时时间 valid_referers none blocked server_names *.test.com ; if ($invalid_referer) { return 403; } access_log off; # 关闭日志记录 }
8.1. 需求:访问/admin/目录的请求,只容许某几个IP访问,配置以下:
location /admin/ { allow 192.168.133.1; allow 127.0.0.1; deny all; }
mkdir /data/wwwroot/test.com/admin/
echo “test,test”>/data/wwwroot/test.com/admin/1.html
-t && -s reload
curl -x127.0.0.1:80 test.com/admin/1.html -I
curl -x192.168.133.130:80 test.com/admin/1.html -I
8.2. 能够匹配正则
location ~ .*(abc|image)/.*\.php$ { deny all; }
8.3. 根据user_agent限制
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato') { return 403; # “deny all”和“return 403”效果同样 }
9.1. 配置以下:
location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; # fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /opt/www/html$fastcgi_script_name; }
fastcgi_pass 用来指定php-fpm监听的“地址:端口”或者socket
10.1. Nginx 代理示意图:
10.2. Nginx 代理配置:
cd /usr/local/nginx/conf/vhost
vim proxy.conf
//加入以下配置
server { listen 80; # 监听端口 server_name ask.apelearn.com; # 访问域名 location / { proxy_pass http://121.201.9.155/; # 代理的网站IP地址 proxy_set_header Host $host; # 代理的网站域名,Host == server_name proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
负载均衡配置:
vim /usr/local/nginx/conf/vhost/load.conf
// 写入以下内容
upstream qq_com # “upstream”来指定多个“web server” { ip_hash; # 做用:确保同个客户端访问在同一个IP server 61.135.157.156:80; server 125.39.240.113:80; } server { listen 80; server_name www.qq.com; location / { proxy_pass http://qq_com; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
Nginx 不支持代理 https ;但能够在Nginx代理上设置https 监听
12.1. SSL工做流程
- 浏览器发送一个https的请求给服务器;
- 服务器要有一套数字证书,能够本身制做(后面的操做会具体介绍),也能够向组织申请,区别就是本身颁发的证书须要客户端验证经过,才能够继续访问,而使用受信任的公司申请的证书,则不会弹出>提示页面,这套证书其实就是一对公钥和私钥;
- 服务器会把公钥传输给客户端;
- 客户端(浏览器)收到公钥后,会验证其是否合法有效,无效会有警告提醒,有效则会生成一串随机数,并用收到的公钥加密;
- 客户端把加密后的随机字符串传输给服务器;
- 服务器收到加密随机字符串后,先用私钥解密(公钥加密,私钥解密),获取到这一串随机数后,再用这串随机字符串加密传输的数据(该加密为对称加密,所谓对称加密,就是将数据和私钥也就是这个随机字符串>经过某种算法混合在一块儿,这样除非知道私钥,不然没法获取数据内容);
- 服务器把加密后的数据传输给客户端;
- 客户端收到数据后,再用本身的私钥也就是那个随机字符串解密;
12.2 SSL 工做示意图
12.3. 生成SSL密钥对
cd /usr/local/nginx/conf
openssl genrsa -des3 -out tmp.key 2048
//key文件为私钥openssl rsa -in tmp.key -out admin.key
//转换key,取消密码rm -f tmp.key
openssl req -new -key admin.key -out admin.csr
//生成证书请求文件,须要拿这个文件和私钥一块儿生产公钥文件openssl x509 -req -days 365 -in admin.csr -signkey admin.key -out admin.crt
// 这里的admin.crt为公钥12.4. Nginx配置 SSL
vim /usr/local/nginx/conf/vhost/ssl.conf
//加入以下内容server { listen 443; server_name admin.com; index index.html index.php; root /data/wwwroot/admin.com; ssl on; ssl_certificate admin.crt; ssl_certificate_key admin.key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; }
-t && -s reload
//若报错unknown directive “ssl” ,须要从新编译nginx,加上“--with-http_ssl_module”mkdir /data/wwwroot/aming.com
echo “ssl test page.”>/data/wwwroot/admin.com/index.html