Nginx安裝&默認虛擬主機&Nginx用戶認證&Nginx域名重定向

12.6 Nginx安裝

下載Nginx源碼包

可去官網(http://nginx.org)下載至Windows,用rz命令上傳php

[root@linux-10 ~]# cd /usr/local/src
[root@linux-10 src]# rz

解壓縮

[root@linux-10 src]# tar -zxvf nginx-1.14.0.tar.gz

初始化

[root@linux-10 src]# cd nginx-1.14.0
[root@linux-10 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx

注:初始化時最好根據本身的需求增長相關模塊的編譯參數,這裏因爲課程須要暫不加參數。html

編譯&&安裝

make &&  make install

核心文件目錄

一樣支持-t選項linux

[root@linux-10 nginx-1.14.0]# ls /usr/local/nginx/sbin/
nginx
[root@linux-10 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

建立啓動腳本

vim /etc/init.d/nginx //複製以下內容
(參考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx )

修改啓動腳本文件權限

chmod 755 /etc/init.d/nginx

將服務添加至系統服務列表並設置開機啓動

chkconfig --add nginx 
chkconfig nginx on

修改Nginx配置文件

將原配置文件重命名,建立一個新的配置文件nginx

[root@linux-10 nginx-1.14.0]# cd /usr/local/nginx/conf
[root@linux-10 conf]# mv nginx.conf nginx.conf.bak
vim nginx.conf //寫入以下內容
(參考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/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;                    //定義pid
worker_rlimit_nofile 51200;                             //定義最大能夠打開的文件數量
events
{
    use epoll;
    worker_connections 6000;                            //定義最大鏈接數
}

注:上述僅是部分代碼git

fastcgi_pass unix:/tmp/php-fcgi.sock;

server配置中的監聽端口要與PHP中的配置文件保持一致vim

檢查並啓動Nginx服務

[root@linux-10 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@linux-10 conf]# /etc/init.d/nginx  start
Starting nginx (via systemctl):                            [  肯定  ]

查看80端口是否啓用

[root@linux-10 conf]# netstat -lntp |grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      4066/nginx: master

測試PHP解析

在/usr/local/nginx/html/下編寫測試文件1.phpcurl

vim 1.php
<?php
echo "test";

訪問測試tcp

[root@linux-10 html]# curl localhost/1.php 
test

12.7 默認虛擬主機

由於Nginx支持include語法,因此能夠在配置文件中定義虛擬主機配置文件(相似於Apache)工具

修改Nginx配置文件

刪除原有配置文件中的server部分,增長包含虛擬主機配置文件的規則測試

建立相應目錄

mkdir /usr/local/nginx/conf/vhost

編輯虛擬主機配置文件

server
{
    listen 80 default_server;  // 有這個標記的就是默認虛擬主機
    server_name lem.com;       // 指定網站域名
    index index.html index.htm index.php;  //指定索引頁
    root /data/wwwroot/default;            //指定網站存放位置
}

建立網站存放目錄

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

編輯測試文件

vim index.html
This is a default site.

檢測配置文件,無誤後從新加載 

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload

注:從新加載相對於重啓服務而言,它在配置文件有錯誤時,將不會從新加載,即不會破壞原有正在運行的Nginx服務。

測試連通性

[root@linux-10 conf]# curl localhost
This is a default site.
[root@linux-10 conf]# curl -x127.0.0.1:80 123.com
This is a default site.

12.8 Nginx用戶認證

建立虛擬主機

在vhost目錄下,每個虛擬主機配置文件就是一個虛擬主機

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;
}
}

生成密碼文件

生成密碼文件須要藉助Apache的htpasswd工具進行生成,若是本機存在Apache可直接使用,不然須要yum安裝。

yum install -y httpd

生成密碼文件

htpasswd -c /usr/local/nginx/conf/htpasswd lem

測試配置並從新加載

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload

效果測試

[root@linux-10 vhost]# curl -x 127.0.0.1:80 test.com -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.14.0
Date: Fri, 08 Jun 2018 04:28:28 GMT
Content-Type: text/html
Content-Length: 195
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"

針對目錄進行用戶認證

location  /admin/                 //在配置文件中填寫相應目錄
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}
}

針對網頁進行用戶認證

location  ~ admin.php       //location爲定位語句,匹配指定內容
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}
}

12.9 Nginx域名重定向

編輯虛擬主機配置文件

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;
    }
}

Nginx中支持識別多個域名,無需使用別名

permanent爲永久重定向,狀態碼爲301,若是寫redirect則爲302

檢測&&從新加載

ctrl+r :-t
ctrl+r :-s

結果測試

[root@linux-10 vhost]# curl -x 127.0.0.1:80 test2.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0
Date: Fri, 08 Jun 2018 04:51:34 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://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  

相關文章
相關標籤/搜索