Nginx安裝、默認(rèn)虛擬主機(jī)、Nginx用戶認(rèn)證、Nginx域名重定向

目錄

一、Nginx安裝
二、默認(rèn)虛擬主機(jī)
三、Nginx用戶認(rèn)證
四、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
//寫入以下內(nèi)容

#!/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  //更改權(quán)限為755
[root@minglinux-01 /usr/local/src/nginx-1.12.2] chkconfig --add nginx //加入系統(tǒng)服務(wù)
[root@minglinux-01 /usr/local/src/nginx-1.12.2] chkconfig  nginx on //開機(jī)啟動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
寫入以下內(nèi)容

user nobody nobody;    //運行nginx的用戶為nobody,
worker_processes 2;  //nginx的子進(jìn)程數(shù)量
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; //最大連接進(jìn)程數(shù)
}

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對應(yīng)一個虛擬主機(jī),就像apache中的每個virtualhost對應(yīng)一個虛擬主機(jī),server部分未配置則無法監(jiān)聽80端口
    {
        listen 80;
        server_name localhost;
        index index.html index.htm index.php;
        root /usr/local/nginx/html;  //網(wǎng)站根目錄

        location ~ \.php$  //解析php相關(guān)內(nèi)容
        {
            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  //關(guān)閉httpd進(jìn)程
[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通常父進(jìn)程用戶為root,子進(jìn)程用戶為nobody
php-fpm通常父進(jìn)程用戶為root,子進(jìn)程用戶為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解析成功

二、默認(rèn)虛擬主機(jī)

  • 修改主配置文件nginx.conf,/刪除原先server的配置,在結(jié)束符號}上面加入一行配置,如下所示:
 include vhost/*.conf;  
}
  • 在conf目錄下創(chuàng)建一個vhost子目錄
[root@minglinux-01 /usr/local/nginx/conf] pwd
/usr/local/nginx/conf
[root@minglinux-01 /usr/local/nginx/conf] mkdir vhost

  • vhost目錄下創(chuàng)建aaa.com.conf
    寫入以下內(nèi)容
server
{
    listen 80 default_server;  // 有這個標(biāo)記的就是默認(rèn)虛擬主機(jī)
    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
//創(chuàng)建/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是默認(rèn)虛擬主機(jī)

  1. vhost目錄下虛擬主機(jī)配置文件中可以用default_server指定該虛擬主機(jī)為默認(rèn)虛擬主機(jī)。
  2. 如果目錄下有多個虛擬主機(jī)配置文件都沒有使用default_server指定為默認(rèn),那排序靠前的.conf配置文件的虛擬主機(jī)為默認(rèn)虛擬主機(jī)。

三、Nginx用戶認(rèn)證

  1. 整站做用戶認(rèn)證
[root@minglinux-01 ~] vim /usr/local/nginx/conf/vhost/test.com.conf  
server          //創(chuàng)建新的虛擬主機(jī)
{
    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. 部分目錄做用戶認(rèn)證
[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做用戶認(rèn)證
[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為永久重定向,相當(dāng)于httpd的R=301。另外還有一個常用的redirect,相當(dāng)于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

擴(kuò)展

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容