标签:lock echo 谷歌浏览器 block com ... memcached .com tool
一、初识Nginx软件Nginx是一款非常优秀的web服务软件,不但可以做web服务软件,还可以做反向代理负载均衡和前端业务的缓存服务
[root@jiangjunwang ~]# yum install -y pcre-devel openssl-devel
说明:pcre-devel: perl语言正则表达式兼容软件包、openssl-devel:使系统支持https方式访问
[root@jiangjunwang ~]# useradd www -s /sbin/nologin/ -M
[root@jiangjunwang ~]# mkdir /server/tools -p
[root@jiangjunwang ~]# cd /server/tools/
[root@jiangjunwang tools]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
[root@jiangjunwang tools]# tar xf nginx-1.12.2.tar.gz
[root@jiangjunwang nginx-1.12.2]# ./configure --prefix=/application/nginx-12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
说明:
[root@jiangjunwang nginx-1.12.2]# make && make install
[root@jiangjunwang nginx-1.12.2]# ln -s /application/nginx-12.2 /application/nginx
[root@jiangjunwang nginx-1.12.2]# /application/nginx/sbin/nginx
出现以上界面表示nginx安装成功建议使用谷歌浏览器进行访问
简化配置文件
[root@jiangjunwang conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf
b
sed -n ‘10,21p‘ nginx.conf>../conf/extra/www.conf
编辑主配置文件
[root@jiangjunwang conf]# vim /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include extra/www.conf;
include extra/bbs.conf;
}
编辑虚拟机配置文件
[root@jiangjunwang html]# vim /application/nginx/conf/extra/bbs.conf
server {
listen 80;
server_name bbs.av.org;
location / {
root html/bbs;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}[root@jiangjunwang html]# vim /application/nginx/conf/extra/www.conf
server {
listen 80;
server_name www.av.org;
location / {
root html/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
编写测试index.html文件
[root@jiangjunwang html]# echo "bbs.av.org">/application/nginx/html/bbs/index.html
[root@jiangjunwang html]# echo "www.av.org">/application/nginx/html/www/index.html
检查配置文件语法并重启服务
[root@jiangjunwang html]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-12.2/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-12.2/conf/nginx.conf test is successful
[root@jiangjunwang html]# /application/nginx/sbin/nginx -s reload
**编写本机hosts文件并测试
看到如上效果表示一个基于域名的nginx虚拟主机配置完成
所谓主机别名就是让一个IP地址对应多个域名主机,这也是在实际应用中常用的功能
配置方法接着上边的配置我们以bbs.av.com站点为例配置一个别名为bbs.a.com
[root@jiangjunwang extra]# vim bbs.conf
server {
listen 80;
server_name bbs.av.org bbs.a.com;
location / {
root html/bbs;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
然后重启nginx进行访问测试即可
[root@jiangjunwang conf]# cat nginx.conf
worker_processes 1;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include extra/www.conf;
include extra/bbs.conf;
}
编辑主配置文件先定义一个log_format日志格式的参数
[root@jiangjunwang conf]# vim nginx.conf
worker_processes 1;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" "$http_x_forwarded_for"‘;
include extra/www.conf;
include extra/bbs.conf;
}
然后编辑bbs站点目录文件使其使用主配置文件定义的格式生成相应站点的访问日志
[root@jiangjunwang extra]# vim bbs.conf
server {
listen 80;
server_name bbs.av.org bbs.a.com;
location / {
root html/bbs;
index index.html index.htm;
}
access_log logs/bbs.access.log main;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
最后检查语法重启nginx服务即可
在实际生产环境中access日志会逐渐变大,等大到一定程度的时候会打不开这个日志文件,所以需要我们定期的对日志文件进行切割
[root@jiangjunwang logs]# vim /server/scripts/cut_access.sh
#!/bin/bash
data_info=$(date +%F-%H:%M)
mv /application/nginx/logs/bbs.access.log /application/nginx/logs/access.log.$data_info
/application/nginx/sbin/nginx -s reload#cut nginx log cron
00 00 * * * /bin/sh /server/scripts/cut_log.sh &>/dev/null
location 指令的作用是根据用户请求的uri来执行不同的应用或者炒作
location基本的语法为
= --- 精确匹配网站uri资源信息
~ --- 区分大小写匹配网站uri资源信息
~* --- 不区分大小写匹配网站uri资源信息
^~ --- 优先匹配网站uri资源信息
/AV/ --- 指定匹配网站资源目录信息
/ --- 默认匹配网站资源信息
! --- 对匹配的内容进行取反
一个栗子
需求:内网用户可以访问www站点AV目录而外网用户不能访问
编写www站点文件
[root@jiangjunwang extra]# vim www.conf
server {
listen 80;
server_name www.av.org;
root html/www;
index index.html index.htm;
location /AV {
allow 172.16.1.0/24;
deny 10.0.0.0/24;
}
}
[root@jiangjunwang www]# echo "AV info" >AV/oldboy.html
检查语法重启nginx服务分别用内网172网段和外网10网段访问测试即可
Nginx rewrite主要的功能就是实现URL地址重写,Nginx rewrite需要pcre软件的支持,通过prel兼容的正则表达式语法进行规则匹配;
一个栗子
通过rewrite模块实现访问av.org自动跳转到bbs.av.org
编辑bbs站点配置文件
方法一
[root@jiangjunwang extra]# vim bbs.conf
server {
listen 80;
server_name av.org;
root html/bbs;
index index.html index.html;
rewrite ^/(.*) http://bbs.av.org/$1 permanent;}
server {
listen 80;
server_name bbs.av.org bbs.a.com;
location / {
root html/bbs;
index index.html index.htm;
}
access_log logs/bbs.access.log main;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;}
}
然后检查语法重启nginx服务即可
方法二
通过location区块和rewrite结合实现
[root@jiangjunwang extra]# vim bbs.conf
server {
listen 80;
server_name bbs.av.org bbs.a.com;
location / {
root html/bbs;
index index.html index.htm;
if ($host ~ "^av.org$") {
rewrite ^/(.) http://bbs.bbs.org/$1 permanent;
}
access_log logs/bbs.access.log main;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;}
}
}
~
然后检查语法重启nginx服务即可
标签:lock echo 谷歌浏览器 block com ... memcached .com tool
原文地址:http://blog.51cto.com/13268236/2308099