Nginx安装

Nginx安装,默认虚拟主机以及认证和重定向
1.首先下载安装包

[root@weixing01 src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
--2018-03-14 00:46:57--  http://nginx.org/download/nginx-1.12.2.tar.gz
正在解析主机 nginx.org (nginx.org)... 206.251.255.63, 95.211.80.227, 2606:7100:1:69::3f, ...
正在连接 nginx.org (nginx.org)|206.251.255.63|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK

2.解压缩

[root@weixing01 src]# tar zxf nginx-1.12.2.tar.gz 
[root@weixing01 src]# cd nginx-1.12.2/

3.编译

[root@weixing01 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx

4.接下来安装:

make
make install

5.安装完成:

[root@weixing01 nginx-1.12.2]# ls /usr/local/nginx/
conf  html  logs  sbin

6.制作启动脚本:

[root@weixing01 nginx-1.12.2]# 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=$?

7.更改权限:

[root@weixing01 nginx-1.12.2]# chmod 755 /etc/init.d/nginx
[root@weixing01 nginx-1.12.2]# chkconfig --add nginx
[root@weixing01 nginx-1.12.2]# chkconfig nginx on

Nginx安装,默认虚拟主机以及认证和重定向

8.更改配置文件:

[root@weixing01 conf]# vim nginx.conf

user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{   
    use epoll;
    worker_connections 6000;
}
http
{   
    include mime.types;

9.启动服务:

[root@weixing01 conf]# /etc/init.d/nginx start
Starting nginx (via systemctl):                            [  确定  ]
[root@weixing01 conf]# ps aux |grep nginx
root       4772  0.0  0.0  20496   624 ?        Ss   01:10   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody     4773  0.0  0.3  22940  3212 ?        S    01:10   0:00 nginx: worker process
nobody     4774  0.0  0.3  22940  3212 ?        S    01:10   0:00 nginx: worker process
root       4776  0.0  0.0 112676   984 pts/0    R+   01:11   0:00 grep --color=auto nginx

Nginx默认虚拟主机

Nginx安装,默认虚拟主机以及认证和重定向
1.首先修改配置文件:

[root@weixing01 conf]# vim nginx.conf
    application/xml;
    include vhost/*.conf;
}
~                           

2.创建默认主机:

[root@weixing01 conf]# mkdir vhost
[root@weixing01 conf]# cd vhost/
[root@weixing01 vhost]# ls
[root@weixing01 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;
}

3.测试并重新加载:

[root@weixing01 default]# /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@weixing01 default]# /usr/local/nginx/sbin/nginx -s reload

4.验证:

[root@weixing01 default]# curl localhost
This is the default site 

用户认证

Nginx安装,默认虚拟主机以及认证和重定向

1.做虚拟主机配置文件:

[root@weixing01 vhost]# vim 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;
}
}

2.生成密码文件:

[root@weixing01 vhost]# /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd weixing
New password: 
Re-type new password: 
Adding password for user weixing
[root@weixing01 vhost]# cat /usr/local/nginx/conf/htpasswd 
weixing:$apr1$bCpQS9At$I6X83gZ8dJ2f1Z3.e1y.c1

3.测试,重新加载:

[root@weixing01 vhost]# /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@weixing01 vhost]# /usr/local/nginx/sbin/nginx -s reload

4.验证:

[root@weixing01 vhost]# curl -x127.0.0.1:80 test.com
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>
[root@weixing01 vhost]# curl -u weixing:3914 -x127.0.0.1:80 test.com
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>

创建目录后重新测试:

[root@weixing01 vhost]# ls /data/wwwroot/test.com
ls: 无法访问/data/wwwroot/test.com: 没有那个文件或目录
[root@weixing01 vhost]# mkdir /data/wwwroot/test.com
[root@weixing01 vhost]# echo "test.com" > /data/wwwroot/test.com/index.html
[root@weixing01 vhost]# curl -u weixing:3914 -x127.0.0.1:80 test.com
test.com

Nginx域名重定向

Nginx安装,默认虚拟主机以及认证和重定向

1.修改配置文件:

server
{
    listen 80;
    server_name test.com test2.com test3.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
    if ($host != 'test.com' ) {
        rewrite  ^/(.*)$  http://test.com/$1  permanent;
    }

    location  /
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
    }
}

2.测试:

[root@weixing01 vhost]# curl -x127.0.0.1:80 test2.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Tue, 13 Mar 2018 17:53:50 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html