linux学习第四十四篇:Nginx安装,Nginx默认虚拟主机,Nginx域名重定向

Nginx安装

./configure --prefix=/usr/local/nginx
  • 编译安装:
    make && make install

  • 编辑Nginx启动脚本:
    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

  • 添加Nginx服务:
    chkconfig –add nginx

  • 设置开机启动:
    chkconfig nginx on

  • 配置Nginx的配置文件,因为Nginx下已经有nginx.conf这个配置文件,我们不用它,把它备份起来然后用自己的配置:
    cd /usr/local/nginx/conf/
    mv nginx.conf nginx.conf.bak

  • 编辑配置文件:
    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; //定义Nginx最多可以打开多少个文件
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 //每一个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;
        }    
    }
}
  • 测试配置文件语法:
    /usr/local/nginx/sbin/nginx -t

  • 开启Nginx:
    /etc/init.d/nginx start

  • Nginx对应的是80端口:
    netstat -lntp |grep 80

  • 测试Nginx解析php:
    vi /usr/local/nginx/html/1.php
    加入如下内容:

 <?php
    echo "test php scripts.";
?>
  • 用curl测试:
    curl localhost/1.php
    这里写图片描述

Nginx默认虚拟主机

  • 编辑配置文件:
    vim /usr/local/nginx/conf/nginx.conf
    增加:
include vhost/*.conf

这里写图片描述

  • 创建vhost目录:
    mkdir /usr/local/nginx/conf/vhost

  • 进入vhost目录下并创建编辑一个.conf文件:
    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;
}
  • 创建default目录:
    mkdir /data/wwwroot/default/

  • 写一些内容在default目录下的index.html文件中:
    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测试:
    curl localhost
    curl -x127.0.0.1:80 123.com
    就算访问的不是aaa.com,只要解析过来,指向到我们服务器,都能访问到这个站点,这就是默认虚拟主机。
    这里写图片描述

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  /
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}
}
  • 创建test.com目录:
    mkdir /data/wwwroot/test.com

  • 在test.com目录下编辑index.html:
    echo “test.com”>/data/wwwroot/test.com/index.html

  • 如果之前没有安装过Apache的话就安装httpd,为了是可以使用Apache的htpasswd工具:
    * yum install -y httpd*

  • Apache自带命令htpasswd创建密码文件,-c是创建,-m是指定md5加密类型,指定用户为xie(PS:如果再次新增用户,就不需要再加 -c ,因为已经创建过密码文件了):
    /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd xie
    这里写图片描述

  • 测试配置并重新加载:
    /usr/local/nginx/sbin/nginx -t
    /usr/local/nginx/sbin/nginx -s reload

  • curl测试:
    curl -x127.0.0.1:80 test.com
    这里出现401,说明需要用户密码认证
    这里写图片描述
    当我们用-u加上用户名和密码时就可以访问了
    这里写图片描述

  • 针对目录的用户认证:
    在location后面加上目录名字即可
    这里写图片描述
    用curl测试,访问网站时是正常的,只有访问admin目录下时就会出现401,需要用户认证
    这里写图片描述
    当我们指定用户密码后就可以正常访问了
    这里写图片描述

  • 针对文件的用户认证:
    在location后面加上匹配文件名字,下图的意思就是匹配到admin.php这个文件就需要用户认证
    这里写图片描述
    curl测试,访问admin目录时是正常的,访问admin.php就需要用户认证了
    这里写图片描述

Nginx域名重定向

  • 更改test.com.conf
server
{
    listen 80;
    server_name test.com test1.com test2.com;
    //server_name后面支持写多个域名,这里要和httpd的做一个对比
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
    if ($host != 'test.com' ) {
        rewrite  ^/(.*)$  http://test.com/$1  permanent; 
        //permanent为永久重定向,状态码为301,如果写redirect则为302
    }
}

这里写图片描述

  • 设置好后-t,-s测试加载配置文件

  • curl测试:
    访问test2.com后会跳转到test.com
    这里写图片描述

扩展
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

发布了79 篇原创文章 · 获赞 5 · 访问量 3万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章