12.6-12.9 Nginx安装,默认虚拟主机,域名重定向

12.6 Nginx安装

大纲

blob.png

blob.png

1 进入src目录,把nginx下载在此目录

 #cd  /usr/local/src

 #wget http://nginx.org/download/nginx-1.8.0.tar.gz

2 解压压缩包

 #tar zxf nginx-1.12.1.tar.gz

3 进行编译,安装

 #./configure --prefix=/usr/local/nginx

 #make &&  make install

nginx的核心程序,也可以利用-t去检查状态。

[root@AliKvn usr]# ls /usr/local/nginx/sbin/nginx 

/usr/local/nginx/sbin/nginx

[root@AliKvn usr]# /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

4 编辑nginx配置文件

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

blob.png

#!/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

5 更改文件755权限

 #chmod 755 /etc/init.d/nginx

6 添加开机启动服务

 #chkconfig --add nginx 

 #chkconfig nginx on

7 配置Nginx的配置文件

 #cd /usr/local/nginx/conf/ 

 #mv nginx.conf nginx.conf.1

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

blob.png

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;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm 
    application/xml;
    server
    {
        listen 80;
        server_name localhost;
        index index.html index.htm index.php;
        root /usr/local/nginx/html;
        location ~ \.php$ 
        {
            include fastcgi_params;
            fastcgi_pass unix:/tmp/php-fcgi.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
        }    
    }
}

配置文件参数解析:

user 定义启动Nginx指哪个用户

worker_processes 2  启动进程有2个子进程

worker_rlimit_nofile 51200 Nginx最多可以打开的文件数51200

use epoll;使用epoll模式

worker_connections 6000进程最多有6000个链接

server部分对应httpd的v-host虚拟主机

server_name 域名

location php 解析php相关参数部分

root 网页路径

blob.png

!!!!一般监听80端口出错或者不通,大多数是跟server这部分配置参数有密切关系,还有可能httpd也被启动了导致80端口被占用。

8 编辑完成后,检查状态,进程以及端口

[root@AliKvn 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

检查有无错误,尝试启动服务

(注意,如果httpd启动了会导致80端口被占用,所以必须要关闭,否则nginx启动失败)。

失败报错一般为如下内容,

blob.png

遇到这种情况,需要把httpd关闭掉,然后再继续启动nginx即可。

[root@AliKvn conf]# /etc/init.d/nginx start

Starting nginx (via systemctl):                            [  OK  ]

 #ps aux |grep nginx 检查进程,有2个子进程(两个nobody)

Ss表示父进程,一般父进程user都是root,子进程都是nobody

blob.png

检查端口,

#netstat -lntp |grep 80

blob.png

9 测试php解析

建立php文件

#vim /usr/local/nginx/html/1.php

<?php
echo "this is the Nginx test page.";

curl检查php解析测试

[root@AliKvn conf]# curl 127.0.0.1/1.php

this is the Nginx test page.[root@AliKvn conf]# 



12.7 Nginx默认虚拟主机

大纲

blob.png

在Nginx中也有默认虚拟主机,跟httpd类似,第一个被Nginx加载的虚拟主机就是默认主机。

和httpd不同的是,在Nginx中,可以利用default_server来标记默认虚拟主机,如果虚拟主机没被标记默认,则第一个虚拟主机会被选为默认虚拟主机。

相关配置操作

1 修改nginx的配置文件,在里面增加参数include vhost/*.conf 

[root@AliKvn conf]#vim /usr/local/nginx/conf/nginx.conf 

blob.png 

blob.png

参数解释:

include vhost/*.conf 

表示支持vhost/所有*.conf的文件,include支持通配使用


2 建立vhost目录

[root@AliKvn conf]#  mkdir vhost

[root@AliKvn 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;
}

解析

server

{

    listen 80 default_server;  // 有这个标记的就是默认虚拟主机

    server_name aaa.com;//站点名字

    index index.html index.htm index.php;//相关索引页

    root /data/wwwroot/default;//站点目录

}

3 建立default目录,并进入,定义新html,创建索引页index.html

[root@AliKvn vhost]# mkdir -p /data/wwwroot/default/

[root@AliKvn vhost]# echo “This is a default site.”>/data/wwwroot/default/index.html

-t检查状态

[root@AliKvn vhost]# /usr/local/nginx/sbin/nginx -t

-s重新加载,这里相当于apache的graceful用法

[root@AliKvn vhost]# /usr/local/nginx/sbin/nginx -s reload

4 测试

测试默认站点

[root@AliKvn vhost]# curl localhost

this is a default site. 

测试默认主机

[root@AliKvn vhost]# curl -x127.0.0.1:80 aaa.com

this is a default site. 

[root@AliKvn vhost]# curl -x127.0.0.1:80 aaa1111.com

this is a default site. 


12.8 Nginx用户认证

大纲

blob.png

blob.png

[root@AliKvn vhost]# cd /usr/local/nginx/conf/vhost

[root@AliKvn vhost]#vim /usr/local/nginx/conf/vhost/test.com.conf

1 写入如下内容,建立认证参数(其实认证参数都是一样的,只是个别参数不同,例如:server name root.其他致一样)

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_basic_user_file 用来定义用户认证的文件以及密码,此处一般接密码文件


2 使用htpasswd工具生成密码(如果没有htpasswd密码工具的话,可以安装httpd yum install -y httpd)

用法,生成文件 指定用户

[root@AliKvn vhost]# /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd aming

第二个用户建立****(注意-c是生成建立,如果建立一次后,就不要再用-c去创建,否则会清零然后重置密码)

[root@AliKvn vhost]# /usr/local/apache2.4/bin/htpasswd  /usr/local/nginx/conf/htpasswd user1

3 检查并重新加载, -t && -s reload 

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

4 curl测试,状态码是401未认证。

blob.png

curl指定用户测试

blob.png

如果想访问test.com/下面目录的站点,可以做这样的配置

访问/test.com/admin/认证

1 修改location后面的路径,定义为

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

2 检查并重新加载, -t &&  -s reload 

3 curl测试

#curl -uaming:passwd -x127.0.0.1 test.com/admin/

访问/test.com/admin.php认证 提示认证  状态码401

curl -uaming:passwd -x127.0.0.1 test.com/admin.php


12.9 Nginx域名重定向

Nginx的域名重定向和httpd类似,但更容易理解,操作如下:

1 修改配置文件test.com.conf

server
{
    listen 80;
    server_name test.com test1.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;
    }
}

参数解析:

 rewrite  ^/(.*)$  http://test.com/$1  permanent;

^/(.*)$其实就等于http://$host/(.*)$ ,那么http://$host/ ,相当于http://test.com/

/(.*)$ /表示站点下面的子页,(.*)$表示通配后面的字符串,直到最后,$表示最后。

$1 = (.*)$

$host/可以省略,直接变成^/,意思是以xxx开头,

permanent表示301

!!!!

在Nginx里,server_name后面支持写多个域名。但在httpd里,httpd只能带一个server name,多个域名只能用Alias Name来定义。

permanent为永久重定向,状态码为301,如果写redirect则为302

2 检查配置语法并重新加载 -t&&-s reload

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

3 curl测试

先访问以下主域名test.com 状态码200,表示通过访问

blob.png

访问test1.com ,即非test.com。访问代码301,这是因为^参数起了作用了。

blob.png

访问test2.com/111/111,状态码为301,^/(.*)$条件成立。

blob.png

猜你喜欢

转载自blog.51cto.com/13578154/2107156