目录
1.源码安装Nginx,并提供服务脚本
1、安装 Nginx 所需的 pcre 库
yum install -y pcre-devel
(1)检查并安装 Nginx 基础依赖包 pcre-devel 、openssl-devel
rpm -qa | egrep 'pcre-devel | openssl-devel'
( 2 )
安装
openssl-devel
安装Tengine(淘宝对Nginx二次开发后的)
获取地址:http://tengine.taobao.org/
wget -c http://tengine.taobao.org/download/tengine-2.3.0.tar.gz
(2)解压tengine到指定文件夹
tar xf tengine-2.3.0.tar.gz -C /usr/local/src/
cd /usr/local/src/tengine-2.3.0/
(3)创建用户nginx 此处只设置便于不登录即可
useradd -s /sbin/nginx nginx
(4) 开始安装 Nginx
./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module \
> --with-http_ssl_module
(5)编译安装
make install
(6)测试nginx启动
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
(7)启动nginx
/usr/local/nginx/sbin/nginx
配置服务启动脚本
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
重启服务
systemctl daemon-reload
systemctl start nginx
systemctl status nginx
测试
2、配置基于域名的虚拟主机
(1)创建站点目录
for i in blog bbs
> do
> mkdir html/$i
> done
(2)创建主页文件
or i in blog bbs
> do
> echo "$i test page." > html/$i/index.html
> done
(3)添加模块化配置文件
vim /usr/local/nginx/conf/nginx.conf
http {
#gzip on;
include /usr/local/nginx/conf.d/*.conf;
(4)创建该文件目录
mkdir /usr/local/nginx/conf.d
(5)配置文件添加虚拟主机部分,创建虚拟主机
vim /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name bbs.test.com;
location / {
root html/bbs;
index index.html index.htm;
}
}
server {
listen 80;
server_name blog.test.com;
location / {
root html/blog;
index index.html index.htm;
}
}
(6)在另一台设备上配置hosts域名信息
vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.230.133 bbs.test.com blog.test.com
3. 配置Nginx访问控制。
配置基于地址的访问控制
(1)在虚拟主机中配置基于地址的访问控制
vim conf.d/vhost.conf
server {
listen 80;
server_name bbs.test.com;
location / {
root html/bbs;
index index.html index.htm;
deny 192.168.230.133; #配置需要拒绝访问的地址
allow 192.168.230.0/24; #配置允许地址段
deny all; #拒绝所有其他地址
}
location /nginx_status {
stub_status on;
access_log off;
}
}
基于基于用户的访问控制
(1)添加认证指令
在虚拟主机location中添加两条参数
auth_basic "Restricted";
auth_basic_user_file /usr/local/nginx/webpass;
(2)创建账号密码, 此账号密码就是用户访问网站时需要输入的。
需要安装创建用户工具
yum install httpd-tools
创建用户tom并配置密码
htpasswd -cm /usr/local/nginx/webpass tom
New password:
Re-type new password:
Adding password for user tom
more /usr/local/nginx/webpass
tom:$apr1$UoHoNkgY$qAh73D31vskaCTGHL55m01
配置完成可以在另一条机器上访问,最好是安装文本浏览器来访问elinks
yum install elinks -y
通过elinks访问成功时出现需要账号和密码进行用户认证
elinks http://bbs.test.com/nginx_status
4.配置Nginx的重定向页面跳转
(1)配置虚拟主机中网页信息
vim /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name blog.test.com;
location / {
root html/blog;
index index.html index.htm;
if (!-f $request_filename) {
rewrite /.* /err.html permanent;
}
}
}
(2)为错误页面添加信息
echo "page not found" > /usr/local/nginx/html/blog/err.html