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

目录

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

一、Nginx安装

  • 下载和解压Nginx
[root@minglinux-01 /usr/local/src] wget http://nginx.org/download/nginx-1.12.2.tar.gz
[root@minglinux-01 /usr/local/src] tar zxvf nginx-1.12.2.tar.gz 
  • 初始化配置编译选项
[root@minglinux-01 /usr/local/src] cd nginx-1.12.2/
[root@minglinux-01 /usr/local/src/nginx-1.12.2] ./configure --prefix=/usr/local/nginx    
//指定安装路径

  • 编译和安装Nginx
[root@minglinux-01 /usr/local/src/nginx-1.12.2] make && make install

[root@minglinux-01 /usr/local/src/nginx-1.12.2] ls /usr/local/nginx/
conf  html  logs  sbin    
//conf:配置文件目录
//html:样例文件目录
//logs:存放日志目录
//sbin:核心文件目录

[root@minglinux-01 /usr/local/src/nginx-1.12.2] /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

  • 编写Nginx启动脚本
[root@minglinux-01 /usr/local/src/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=$?
    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

[root@minglinux-01 /usr/local/src/nginx-1.12.2] chmod 755 /etc/init.d/nginx  //更改权限为755
[root@minglinux-01 /usr/local/src/nginx-1.12.2] chkconfig --add nginx //加入系统服务
[root@minglinux-01 /usr/local/src/nginx-1.12.2] chkconfig  nginx on //开机启动Nginx
  • 更改Nginx的配置文件
[root@minglinux-01 /usr/local/src/nginx-1.12.2/conf] mv nginx.conf nginx.conf.1  //不用原有的,新建一个
[root@minglinux-01 /usr/local/src/nginx-1.12.2/conf] vim nginx.conf
写入以下内容

user nobody nobody;    //运行nginx的用户为nobody,
worker_processes 2;  //nginx的子进程数量
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对应一个虚拟主机,就像apache中的每个virtualhost对应一个虚拟主机,server部分未配置则无法监听80端口
    {
        listen 80;
        server_name localhost;
        index index.html index.htm index.php;
        root /usr/local/nginx/html;  //网站根目录

        location ~ \.php$  //解析php相关内容
        {
            include fastcgi_params;
            fastcgi_pass unix:/tmp/php-fcgi.sock;
            #fastcgi_pass 127.0.0.1:9000 
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
        }    
    }
}
  • 检查配置文件然后启动nginx
[root@minglinux-01 /usr/local/src/nginx-1.12.2/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
[root@minglinux-01 /usr/local/src/nginx-1.12.2/conf] /usr/local/apache2.4/bin/apachectl stop  //关闭httpd进程
[root@minglinux-01 /usr/local/src/nginx-1.12.2/conf] /etc/init.d/nginx  start
Starting nginx (via systemctl):                            [  确定  ]
[root@minglinux-01 /usr/local/src/nginx-1.12.2/conf] ps aux |grep nginx
root        430  0.0  0.0  20536   608 ?        Ss   22:06   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody      431  0.0  0.0  20976  1056 ?        S    22:06   0:00 nginx: worker process
root        433  0.0  0.0 112720   984 pts/1    S+   22:07   0:00 grep --color=auto nginx

nginx通常父进程用户为root,子进程用户为nobody
php-fpm通常父进程用户为root,子进程用户为php-fpm

  • 测试是否正确解析PHP
[root@minglinux-01 /usr/local/src/nginx-1.12.2/conf] vim /usr/local/nginx/html/1.php

<?php
    echo "PHP解析成功\n";
?>
[root@minglinux-01 /usr/local/nginx/conf] curl localhost/1.php
PHP解析成功

二、默认虚拟主机

  • 修改主配置文件nginx.conf,/删除原先server的配置,在结束符号}上面加入一行配置,如下所示:
 include vhost/*.conf;  
}
  • 在conf目录下创建一个vhost子目录
[root@minglinux-01 /usr/local/nginx/conf] pwd
/usr/local/nginx/conf
[root@minglinux-01 /usr/local/nginx/conf] mkdir vhost

  • vhost目录下创建aaa.com.conf
    写入以下内容
server
{
    listen 80 default_server;  // 有这个标记的就是默认虚拟主机
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/wwwroot/default;
}
[root@minglinux-01 /usr/local/nginx/conf] mkdir -p /data/wwwroot/default
//创建/data/wwwroot/default
  • 测试
[root@minglinux-01 /data/wwwroot/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@minglinux-01 /data/wwwroot/default] /usr/local/nginx/sbin/nginx -s reload  //重新加载
[root@minglinux-01 /data/wwwroot/default] vim /data/wwwroot/default/index.html
写入
Hello,thank you!

[root@minglinux-01 /data/wwwroot/default] curl localhost
Hello,thank you!
[root@minglinux-01 /data/wwwroot/default] curl -x127.0.0.1:80 aaa.com
Hello,thank you!
[root@minglinux-01 /data/wwwroot/default] curl -x127.0.0.1:80 a.com
Hello,thank you!  //aaa.com是默认虚拟主机

  1. vhost目录下虚拟主机配置文件中可以用default_server指定该虚拟主机为默认虚拟主机。
  2. 如果目录下有多个虚拟主机配置文件都没有使用default_server指定为默认,那排序靠前的.conf配置文件的虚拟主机为默认虚拟主机。

三、Nginx用户认证

  1. 整站做用户认证
[root@minglinux-01 ~] 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;
    }
}

[root@minglinux-01 ~] /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd ming    //生成密码文件,指定一个用户
New password: 
Re-type new password: 
Adding password for user ming
[root@minglinux-01 ~] cat /usr/local/nginx/conf/htpasswd 
ming:$apr1$RbHhD.B/$Cy5W5B1r2535.f5TYJTdL.
[root@minglinux-01 ~] /usr/local/apache2.4/bin/htpasswd  /usr/local/nginx/conf/htpasswd ming1
New password: 
Re-type new password: 
Adding password for user ming1
[root@minglinux-01 ~] cat /usr/local/nginx/conf/htpasswd 
ming:$apr1$RbHhD.B/$Cy5W5B1r2535.f5TYJTdL.
ming1:$apr1$TsZ0Z5jj$.4i0ecCyxoj59p6Hh.nUu.

再次使用htpasswd工具的-c选项生成密码会覆盖之前的账户密码,不加-c则不会覆盖

  • 测试
[root@minglinux-01 ~] /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@minglinux-01 ~] /usr/local/nginx/sbin/nginx -s reload  //重新加载配置,不会重启Nginx

[root@minglinux-01 ~] mkdir /data/wwwroot/test.com
[root@minglinux-01 ~] echo "test page" >/data/wwwroot/test.com/index.html
[root@minglinux-01 ~] curl -uming:123456 -x127.0.0.1:80 test.com 
test page

  1. 部分目录做用户认证
[root@minglinux-01 ~] 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   //在这里改变目录即可
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
    }
}
  • 测试
[root@minglinux-01 ~] /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@minglinux-01 ~] /usr/local/nginx/sbin/nginx -s reload
[root@minglinux-01 ~] curl -x127.0.0.1:80 test.com 
test page
[root@minglinux-01 ~] curl -x127.0.0.1:80 test.com/admin/ -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.12.2
Date: Mon, 26 Nov 2018 13:27:07 GMT
Content-Type: text/html
Content-Length: 195
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"

[root@minglinux-01 ~] curl -uming:123456 -x127.0.0.1:80 test.com/admin/ -I
HTTP/1.1 404 Not Found                    //加上用户名密码后可以访问
Server: nginx/1.12.2
Date: Mon, 26 Nov 2018 13:28:49 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive

  1. 单独url做用户认证
[root@minglinux-01 ~] 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.php   //匹配admin.php
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
    }
}

  • 测试
[root@minglinux-01 ~] /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@minglinux-01 ~] /usr/local/nginx/sbin/nginx -s reload
[root@minglinux-01 ~] curl -x127.0.0.1:80 test.com/admin.php -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.12.2
Date: Mon, 26 Nov 2018 13:46:09 GMT
Content-Type: text/html
Content-Length: 195
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"

[root@minglinux-01 ~] curl -uming:123456 -x127.0.0.1:80 test.com/admin.php
admin page

四、Nginx域名重定向

  • 修改配置文件
[root@minglinux-01 ~] vim /usr/local/nginx/conf/vhost/test.com.conf


{
    listen 80;
    server_name test.com test2.com test3.com;  //server_name后面可以跟多个域名
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
    if ($host != 'test.com' ) {
        rewrite  ^/(.*)$  http://test.com/$1  permanent;
    }   
         //这里的^/(.*)$写全则为http://$host/(.*)$,permanent为永久重定向,相当于httpd的R=301。另外还有一个常用的redirect,相当于httpd的R=302。
    location  ~admin.php
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
    }   
}
  • 测试
[root@minglinux-01 ~] /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@minglinux-01 ~] /usr/local/nginx/sbin/nginx -s reload
[root@minglinux-01 ~] curl -x127.0.0.1:80 test2.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Mon, 26 Nov 2018 14:02:45 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/

[root@minglinux-01 ~] curl -x127.0.0.1:80 test2.com/abcd -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Mon, 26 Nov 2018 14:03:40 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/abcd

扩展

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,547评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,399评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,428评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,599评论 1 274
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,612评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,577评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,941评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,603评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,852评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,605评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,693评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,375评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,955评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,936评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,172评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,970评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,414评论 2 342

推荐阅读更多精彩内容