您的位置:首页 > 运维架构 > Nginx

Nginx安装、默认虚拟主机、 Nginx用户认证、Nginx域名重定向

2018-06-07 22:51 856 查看

12.6 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 //编译,根据需求,加上相应的参数模块,源码包尽量保留,有些模块在源码包里

make && make install

vim /etc/init.d/nginx //复制如下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx ) 启动脚本

/usr/local/nginx/sbin/nginx -t /检查

cd /usr/local/nginx/conf/; mv nginx.conf nginx.conf.bak

vim nginx.conf //写入如下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf)配置文件

user 那个用户来启动服务

worker_processes 定义子进程有几个

worker_rlimit_nofile 最多访问多少个文件

worker_conections 最大的连接数

60行,server 类似虚拟主机

/usr/local/nginx/sbin/nginx -t

/etc/init.d/nginx start

ps aux |grep ngnix //ss父进程 S子进程

curl localhost

vi /usr/local/nginx/html/1.php //加入如下内容

<?php
echo "test php scripts.";
?>

curl localhost/1.php

12.7 默认虚拟主机

vim /usr/local/nginx/conf/nginx.conf //删除server,http下增加 include vhost/*.conf

mkdir /usr/local/nginx/conf/vhost

cd 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;
}

cd vhost

mkdir /data/wwwroot

vim index.html
This is the fault index

/usr/local/ngnix/sbin/ngnix -t

/usr/local/ngnix/sbin/ngnix -s reload /重新加载

curl -x137.0.0.1:80 aaa.com

vhost目录下第一个为默认虚拟主机

加标志位default_server

12.8 Nginx用户认证

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 / //‘/’根目录名,可以换成其他的目录/admin或者匹配针对一个url- admin.php
{
auth_basic "Auth"; //定义用户的名字
auth_basic_user_file /usr/local/nginx/conf/htpasswd; 用户密码
}
}
2. yum install -y httpd
3. htpasswd -c /usr/local/nginx/conf/htpasswd aming //生成密码
4. -t && -s reload //测试配置并重新加载
5. curl -uaming:lishiming -x127.0.0.1:80 test.com
6. mkdir /data/wwwroot/test.com
7. echo “test.com”>/data/wwwroot/test.com/index.html
8. curl -uaming:lishiming -x127.0.0.1:80 test.com

12.9 Nginx域名重定向

更改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.html

http://my.oschina.net/duxuefeng/blog/34880

nginx rewrite四种flag

http://www.netingcn.com/nginx-rewrite-flag.html

http://unixman.blog.51cto.com/10163040/1711943
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐