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

Nginx安装

Nginx安装

Nginx安装
[root@iZbp1e0xboek6oow616aoiZ src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz
[root@iZbp1e0xboek6oow616aoiZ src]# tar zxvf nginx-1.14.0.tar.gz
[root@iZbp1e0xboek6oow616aoiZ src]# cd nginx-1.14.0
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# ./configure --prefix=/usr/local/nginx
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# echo $?
0
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# make &&  make install
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# echo $?
0
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# 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@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# chmod 755 /etc/init.d/nginx
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# chkconfig --add nginx
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# chkconfig nginx on
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# vim /usr/local/nginx/conf/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;
    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;
        }    
    }
}

[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# /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@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# /etc/init.d/nginx  start
Starting nginx (via systemctl):                            [  确定  ]
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# netstat -lntp |grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      13619/nginx: master

12.7 Nginx默认虚拟主机

Nginx默认虚拟主机
[root@iZbp1e0xboek6oow616aoiZ nginx]# vim /usr/local/nginx/conf/nginx.conf

#删除server,并在http中最后添加include vhost/*.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;
    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;
    include vhost/*.conf;
}

[root@iZbp1e0xboek6oow616aoiZ nginx]# /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@iZbp1e0xboek6oow616aoiZ nginx]# mkdir /usr/local/nginx/conf/vhost
[root@iZbp1e0xboek6oow616aoiZ nginx]# vim  /usr/local/nginx/conf/vhost/aaa.conf

server
{
    listen 80 default_server;
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/wwwroot/default;
}

[root@iZbp1e0xboek6oow616aoiZ nginx]# /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@iZbp1e0xboek6oow616aoiZ nginx]# mkdir -p  /data/wwwroot/default
[root@iZbp1e0xboek6oow616aoiZ nginx]# vim /data/wwwroot/default/index.html

test page

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

Nginx用户认证

[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# 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@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# yum install -y httpd
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# htpasswd -c /usr/local/nginx/conf/htpasswd test
New password:
Re-type new password:
Adding password for user test
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# /usr/local/nginx/sbin/nginx -t && /usr/local/nginx/sbin/nginx  -s reload
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@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# mkdir /data/wwwroot/test.com
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# echo “test.com”>/data/wwwroot/test.com/index.html
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# curl -x127.0.0.1:80 test.com -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.14.0
Date: Tue, 24 Apr 2018 09:10:41 GMT
Content-Type: text/html
Content-Length: 195
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# curl -u test:123456 -x 127.0.0.1:80 test.com -I
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Tue, 24 Apr 2018 09:11:19 GMT
Content-Type: text/html
Content-Length: 15
Last-Modified: Tue, 24 Apr 2018 09:10:29 GMT
Connection: keep-alive
ETag: "5adef485-f"
Accept-Ranges: bytes




server
{
    listen 80;
    server_name test.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;

针对目录的用户认证

[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# vim /usr/local/nginx/conf/vhost/test.com.conf

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

针对文件匹配的用户认证

[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# vim /usr/local/nginx/conf/vhost/test.com.conf

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

Nginx域名重定向

image.png
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;
    }

    location  ~  admin.php
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;

[root@iZbp1e0xboek6oow616aoiZ test.com]# /usr/local/nginx/sbin/nginx -s reload
[root@iZbp1e0xboek6oow616aoiZ test.com]# curl -x127.0.0.1:80 test1.com/test/admin.php -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0
Date: Tue, 24 Apr 2018 13:44:33 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/test/admin.php

扩展
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.htmlhttp://unixman.blog.51cto.com/10163040/1711943

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

推荐阅读更多精彩内容

  • 1.ngnix介绍 ngnix www服务软件 俄罗斯人开发 开源 性能很高 本身是一款静态WWW软件 静态小文件...
    逗比punk阅读 2,075评论 1 6
  • 1.简介:  Nginx:engine X ,2002年,开源,商业版 http协议:web服务器(类似于ht...
    尛尛大尹阅读 1,858评论 0 3
  • Page 1:nginx 服务器安装及配置文件详解 CentOS 6.2 x86_64 安装 nginx 1.1 ...
    xiaojianxu阅读 8,524评论 1 41
  • 作者:市井笔农 我从蛮荒之地而来 我的眼里满是寂寥山水 他们学会了流泪 他的世界只有灰 我不希望世界只有灰 我不想...
    市井笔农阅读 250评论 0 1
  • 我们都在长大了。小时候只知道玩儿,和谁谁关系好,谁谁关系不好。 青春期讨论哪个男生好看,哪个打篮球帅,谁和谁在...
    太阳蛋小姐阅读 212评论 0 0