安装依赖
PCRE - Perl Compatible Regular Expressions, perl正则匹配库
yum install pcre*
openssl 可选
安装nginx
- 第一次安装
./configure --prefix=/usr/local/nginx \
--with-http_stub_ssl_module --with-pcre
make && make install
- 已安装nginx,需要增加模块
./configure --prefix=/usr/local/nginx \
--with-http_stub_ssl_module --with-pcre \
--add-module=/模块目录
make
cp objs/nginx /usr/local/nginx/sbin/nginx
注意不需要make install,防止覆盖配置
启动、关闭、重启nginx
/usr/local/nginx
/usr/local/nginx -s stop
/usr/local/nginx -s reload
常用编译参数
- –with-http_stub_status_module:支持nginx状态查询
- –with-http_ssl_module:支持https
- –with-pcre:为了支持rewrite重写功能,必须制定pcre
- –with-http_gzip_static_module 启用ngx_http_gzip_static_module支持(在线实时压缩输出数据流)
- –with-google_perftools_module 启用ngx_google_perftools_module支持(调试用,剖析程序性能瓶颈)
- –with-http_realip_module http_realip_module http_realip_module http_realip_module http_realip_module http_realip_module 启用 realiprealip realip模块(将用户IP转发给后端服务器)
nginx 配置虚拟主机
配置nginx.conf
server {
server_name a.test.com;
listen 80;
root /data/site/a.test.com;
location / {
}
}
- server{}:配置虚拟主机必须有这个段。
- server_name:虚拟主机的域名,可以写多个域名,类似于别名,比如说你可以配置成 server_name b.test.com c.test.com d.test.com,这样的话,访问任何一个域名,内容都是一样的
- root /data/site/b.test.com:站点根目录,你网站文件存放的地方。注:站点目录和域名尽量一样,养成一个好习惯
location 配置
location [=|~|~*|^~] /uri/ { … }
- = 表示精确匹配,这个优先级也是最高的
- ^~ 表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)
- ~ 表示区分大小写的正则匹配
- ~* 表示不区分大小写的正则匹配(和上面的唯一区别就是大小写)
- !~和!~*分别为区分大小写不匹配及不区分大小写不匹配的正则
- / 通用匹配,任何请求都会匹配到,默认匹配.
root&alias文件路径配置
[root]
语法:root path
默认值:root html
配置段:http、server、location、if[alias]
语法:alias path
配置段:location主要区别: root会将location后面的路径拼到全路径, 而alias不会
实例 1:
location ~ ^ /weblogs/ {
root /data/weblogs/a.test.com;
}
请求: /weblogs/httplogs/a.log
映射到:/data/weblogs/a.test.com/weblogs/httplogs/a.log
- 实例 2:
location ~ ^ /weblogs/ {
alias /data/weblogs/a.test.com/;
}
映射到:/data/weblogs/a.test.com/httplogs/a.log
- alias 注意事项:
- 使用alias时,目录名后面一定要加”/”。
- alias可以指定任何名称。
- alias在使用正则匹配时,必须捕捉要匹配的内容并在指定的内容处使用。
- alias只能位于location块中。