Nginx安装
cd /usr/local/src
wget http://nginx.org/download/nginx-1.12.1.tar.gz
tar zvxf nginx-1.12.1.tar.gz
cd nginx-1.12.1
./configure --prefix=/usr/local/nginx
make && make install
ls /usr/local/nginx //查看目录
conf html logs sbin
/usr/local/nginx/sbin/nginx -t (支持-t,查看配置文件是否有错)
//编写启动脚本
vim /etc/init.d/nginx //复制如下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx )
chmod 755 /etc/init.d/nginx //更改权限
chkconfig --add nginx
chkconfig nginx on //开机启动
cd /usr/local/nginx/conf/
//原来的nginx.conf不用,自定义一个
mv nginx.conf nginx.conf.bak
更改nginx的配置文件
//首先把原来的配置文件清空 > /usr/local/nginx/conf/nginx.conf >单独使用时,可以把一个文本文档快速清空
vim nginx.conf //写入如下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf)
/usr/local/nginx/sbin/nginx -t //保存配置文件后,先检验一下是否有错误
/etc/init.d/nginx start //启动nginx
//如果不能启动,查看/usr/local/nginx/logs/error.log文件,检查是否已启动,命令如下 ps aux |grep nginx
netstat -lntp |grep 80
测试php解析
//首先创建测试文件
vim /usr/local/nginx/html/1.php //加入如下内容
<?php
echo "test php scripts.";
?>
[root@localhost conf]# curl localhost/1.php
test php scripts.
Nginx默认虚拟主机
vim /usr/local/nginx/conf/nginx.conf //修改主配置文件,先暂且删除server部分,在结束符号}上面加入一行配置,如下:
include vhost/*.conf;
mkdir /usr/local/nginx/conf/vhost
cd /usr/local/nginx/conf/vhost
vim aaa.com.conf //加入如下内容
server
{
listen 80 default_server; // 有这个标记的就是默认虚拟主机
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
}
mkdir -p /data/wwwroot/default/
cd /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 //重新加载 或者/etc/init.d/nginx restart 重启
curl localhost //访问aaa.com (curl -x127.0.0.1:80 aaa.com )
curl -x127.0.0.1:80 123.com //访问一个没有定义过的域名,也会访问到
Nginx用户认证
cd /usr/local/nginx/conf/vhost
vim /usr/local/nginx/conf/vhost/test.com.conf //创建一个虚拟主机,写入如下内容
server
{
listen 80;
server_name test.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
location /
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
(其中auth_basic "Auth";打开认证 ,定义用户认证名字
auth_basic_user_file /usr/local/nginx/conf/htpasswd; 指定用户密码文件)
(如果在本机中已经安装过apache,可直接用 /usr/local/apache2.4/bin/htpasswd 这个命令 如果没有,yum install -y httpd)
/usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd nan
cat /usr/local/nginx/conf/htpasswd
(创建第二个的时候,就不用加-c了,如果加,就会重置。)
-t && -s reload //测试配置并重新加载
(reload的好处,如果有错误,不会影响配置文件,而restart则不然)
mkdir /data/wwwroot/test.com
echo “test.com”>/data/wwwroot/test.com/index.html
curl -x127.0.0.1:80 test.com -I //状态码为401说明需要验证
curl -uaming:passwd -x127.0.0.1:80 test.com -I //访问状态码变为200
curl -unan:123123 -x127.0.0.1:80 test.com -I
编辑windows的hosts文件,然后在浏览器中访问test.com会有输入用户、密码的弹窗
针对目录的用户认证
vim test.com.confh
location /admin/
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
Nginx域名重定向
cd /usr/local/nginx/conf/vhost
更改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;
}
}
server_name后面支持写多个域名,这里要和httpd的做一个对比
permanent为永久重定向,状态码为301,如果写redirect则为302
扩展
nginx.conf 配置详解 http://www.ha97.com/5194.htmlhttp://my.oschina.net/duxuefeng/blog/34880
nginx rewrite四种flag http://www.netingcn.com/nginx-rewrite-flag.htmlhttp://unixman.blog.51cto.com/10163040/1711943