Nginx安装
###二进制源码包下载
cd /usr/local/src
wget http://nginx.org/download/nginx-1.12.1.tar.gz
####解压
tar zxf nginx-1.12.1.tar.gz
####初始化
./configure --prefix=/usr/local/nginx
####安装Nginx时报错
./configure: error: the HTTP rewrite module requires the PCRE library.
安装pcre-devel解决问题
yum -y install pcre-devel
错误提示
./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library. You can either disable the module by using
--without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
--with-http_ssl_module --with-openssl=<path> options.
解决办法:
yum -y install openssl openssl-devel
####检测安装
echo $? && make &&make install
####复制如下内容
vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start()
{
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop()
{
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload()
{
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart()
{
stop
start
}
configtest()
{
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
参考配置文件 ####设置权限和开机启动
chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
####新建nginx.conf配置文件 更改
cd /usr/local/nginx/conf/; mv nginx.conf nginx.conf.bak
新建
vim nginx.conf
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start()
{
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop()
{
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload()
{
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart()
{
stop
start
}
configtest()
{
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
####检测配置文件
[root@jiangshan conf]# /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
####启动
[root@jiangshan conf]# /etc/init.d/nginx start
Starting nginx (via systemctl): [ OK ]
[root@jiangshan conf]# netstat -lntp |grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 27822/nginx: master
####测试php解析
vi /usr/local/nginx/html/1.php //加入如下内容
<?php
echo "test php scripts.";
?>
curl localhost/1.php
默认虚拟主机
在nginx的配置文件nginx.conf中有一段server{}配置,这就是一个虚拟主机。如果在这个主机下面还有server{}的配置,那么第一个server{}就是默认主机。但是在nginx.conf中配置server{}并不方便我们管理,我们可以使每个虚拟主机都有一个自己的配置文件。
- 第一个被加载的就是默认虚拟主机
- 设置default_server的也可以是默认虚拟主机
####编辑配置文件
vim /usr/local/nginx/conf/nginx.conf //增加
include vhost/*.conf
- include vhost/*.conf表示虚拟主机配置文件将存放在/usr/local/nginx/conf/vhost中。
- /usr/local/nginx/conf/vhost需要手动创建
####新建虚拟主机配置文件
mkdir /usr/local/nginx/conf/vhost
cd !$; vim default.conf //加入如下内容
server
{
listen 80 default_server; // 有这个标记的就是默认虚拟主机
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
}
- listen定义监听端口
- default_server就是默认虚拟主机的标识
- server_name定义域名
- index定义支持的首页格式
- root定义站点根目录
- 注意后面的分号;
创建默认虚拟主机的根目录
mkdir -p /data/wwwroot/default/
创建测试文件
echo “This is a default site.”>/data/wwwroot/default/index.html
####检测配置生效
检查重新加载配置文件
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
测试
curl localhost
curl -x127.0.0.1:80 123.com
Nginx用户认证
###创建新的虚拟主机 ####创建新的虚拟主机配置文件
vim /usr/local/nginx/conf/vhost/test.com.conf
#创建虚拟主机配置文件,必须以.conf结尾,写入以下内容:
server
{
listen 80;
server_name www.test.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
}
####创建根目录以及index.html
mkdir /data/wwwroot/test.com
echo "this is test.com" >/data/wwwroot/test.com/index.html
###创建用户认证 ####修改虚拟主机配置文件
vim /usr/local/nginx/conf/vhost/test.com.conf
#在server{}中加入以下内容
location /
{
auth_basic "auth";
auth_basic_user_file /data/wwwroot/.htpasswd;
}
- location /表示匹配根目录
- auth_basic表示认证类型
- auth_basic_user_file认证用户保存文件,该文件需要使用httpd的htpasswd命令生成
####测试
curl -x127.0.0.1:80 test.com -I//状态码为401说明需要验证
curl -uaming:passwd 访问状态码变为200
编辑windows的hosts文件,然后在浏览器中访问test.com会有输入用户、密码的弹窗
针对目录的用户认证
location /admin/
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
Nginx域名重定向
在Apache配置文件中,servername后面只能带一个域名。而在nginx配置文件中,server_name后面可以跟多个域名。这就涉及到域名权重问题,为了优化SOE,所以我们需要设置主域名。
- server_name后面支持写多个域名,这里要和httpd的做一个对比
- permanent为永久重定向,状态码为301,如果写redirect则为302
####更改test.com.conf
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' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
}