本文共 3711 字,大约阅读时间需要 12 分钟。
部署环境为 CentOS 6.9,系统信息如下:
[root@moban tools]# cat /etc/redhat-releaseCentOS release 6.9 (Final)[root@moban scripts]# uname -r2.6.32-696.el6.x86_64[root@moban tools]# uname -mx86_64
下载 Nginx 最新版本(例如 1.14.0):
wget http://nginx.org/download/nginx-1.14.0.tar.gz
官网地址:http://nginx.org/
安装必要的编译依赖:
yum install -y pcre-devel openssl-devel gcc
创建非 привileged 用户(例如 www):
useradd -s /sbin/nologin -M www
编译并安装 Nginx:
cd /server/tools/tarxf nginx-1.14.0.tar.gzcd nginx-1.14.0./configure --prefix=/application/nginx-1.14 --user=www --group=www --with-http_stub_status_module --with-http_ssl_modulemake && make install
创建 Nginx 的可执行文件软链接:
ln -s /application/nginx-1.14 /application/nginx
启动服务:
/application/nginx/sbin/nginx
优化主配置文件(nginx.conf):
sed -ri '/#|^$/d' /application/nginx/conf/nginx.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; server { listen 80; server_name www.abc.com; location / { root html/www; index index.html index.htm; } }} 测试配置:
/application/nginx/sbin/nginx -t/application/nginx/sbin/nginx
创建首页文件:
mkdir -p /application/nginx/html/wwwcp /application/nginx/html/index.html /application/nginx/html/www/index.html
编辑主配置文件:
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; server { listen 80; server_name www.abc.com; location / { root html/www; index index.html index.htm; } } server { listen 80; server_name bbs.abc.com; location / { root html/bbs; index index.html index.htm; } } server { listen 80; server_name blog.abc.com; location / { root html/blog; index index.html index.htm; } }} 测试配置:
/application/nginx/sbin/nginx -t/application/nginx/sbin/nginx
重载 Nginx 服务:
/application/nginx/sbin/nginx -s reload
检查状态模块是否启用:
/application/nginx/sbin/nginx -V
查看状态模块配置:
cat /application/nginx/conf/extra/status.conf
配置错误日志:
error_log logs/error.log error;
重载服务后测试:
/application/nginx/sbin/nginx -t/application/nginx/sbin/nginx -s reload
访问测试:
curl www.abc.comcurl bbs.abc.comcurl blog.abc.com
配置端口访问:
server { listen 8080; server_name www.abc.com; location / { root html/www; index index.html index.htm; }} 测试访问:
curl www.abc.com:8080
基于 IP 的虚拟主机配置:
server { listen 10.0.0.7:80; server_name www.abc.com; location / { root html/www; index index.html index.htm; }} 测试监听 IP:
netstat -lnp | grep nginx
默认目录结构:
/application/nginx/html/├── www/│ └── index.html├── bbs/│ └── index.html├── blog/│ └── index.html└── logs/ ├── error.log └── access.log
location 语法解析:
=:精确匹配^~:常规字符串匹配~:区分大小写~*:不区分大小写!:否定匹配示例配置:
location / { return 401;}location / { return 402;}location /documents/ { return 403;}location ^~/images/ { return 404;}location ~*\.gif|\.jpg|\.jpeg$ { return 500;} 限制目录访问:
location / { root html/www; index index.html index.htm; allow 172.16.1.0/24; deny all;}location /AV/ { root html/www; index index.html index.htm; allow 172.16.1.0/24; deny all;} 测试访问:
mkdir -p /application/nginx/html/www/AVecho "web01 www" > /application/nginx/html/www/AV/oldboy.html
通过以上配置,Nginx 可以实现多域名的高效负载均衡与访问控制。
转载地址:http://orcfk.baihongyu.com/