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

Nginx安裝

  • cd /usr/local/src
  • wget http://nginx.org/download/nginx-1.12.1.tar.gz
  • tar zxf nginx-1.12.1.tar.gz
  • ./configure --prefix=/usr/local/nginx
  • make && make install
  • 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
  • cd /usr/local/nginx/conf/; mv nginx.conf nginx.conf.bak
  • vim nginx.conf //寫入以下內容(參考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf
  • /usr/local/nginx/sbin/nginx -t
  • /etc/init.d/nginx start
  • netstat -lntp |grep 80

首先切換到/usr/local/src/目錄下# cd /usr/local/src/php

下載Nginx安裝包html

解壓並切換到nginx-1.12.1目錄下linux

配置nginx

編譯和安裝# make && make installgit

查看nginx目錄vim

建立啓動腳本,添加內容,保存退出windows

更改配置文件的權限,添加和啓動系統服務瀏覽器

拷貝配置文件curl

更改配置文件,添加內容,保存退出工具

user nobody nobody; 定義啓動Nginx的用戶

worker_processes 2; 定義子進程數目

worker_rlimit_nofile 51200; 定義Nginx最多可打開的文件數目

worker_connections 6000; 定義進程最大鏈接數

做爲一個網站的服務,必須監聽一個端口,默認監聽的是80端口

檢查配置文件語法對錯,並啓動nginx服務

查看nginx進程

測試nginx

建立一個php測試文件

解析成功

默認虛擬主機

  • vim /usr/local/nginx/conf/nginx.conf //增長include vhost/*.conf
  • mkdir /usr/local/nginx/conf/vhost
  • cd !$; vim default.conf //加入以下內容

    server

    {

        listen 80 default_server; // 有這個標記的就是默認虛擬主機

        server_name aaa.com;

        index index.html index.htm index.php;

        root /data/wwwroot/default;

    }

  • mkdir -p /data/wwwroot/default/
  • 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 localhost
  • curl -x127.0.0.1:80 123.com

首先切換到usr/local/nginx/conf目錄下#cd /usr/local/nginx/conf

編輯Nginx配置文件

刪除原有server內容

在刪除內容的原有位置添加一行include vhost/*.conf;保存退出

添加一臺虛擬主機

建立目錄,在目錄下新建index.html

檢測配置文件是否存在語法錯誤

從新加載nginx

測試訪問

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;

        }

    }

  • yum install -y httpd
  • htpasswd -c /usr/local/nginx/conf/htpasswd aming
  • -t && -s reload //測試配置並從新加載
  • mkdir /data/wwwroot/test.com
  • echo 「test.com」>/data/wwwroot/test.com/index.html
  • curl -x127.0.0.1:80 test.com -I//狀態碼爲401說明須要驗證
  • curl -uaming:passwd 訪問狀態碼變爲200
  • 編輯windows的hosts文件,而後在瀏覽器中訪問test.com會有輸入用戶、密碼的彈窗
  • 針對目錄的用戶認證

    location /admin/

    {

        auth_basic "Auth";

        auth_basic_user_file /usr/local/nginx/conf/htpasswd;

    }

針對全站

首先切換到usr/local/nginx/conf/vhost/目錄下,新建一個虛擬主機,添加內容,保存退出

location / ,指定設置用戶認證的目錄

auth_basic "Auth", 定義用戶認證的名字

auth_basic_user_file /usr/local/nginx/conf/htpasswd,用戶名密碼文件

生成密碼文件,須要用到Apache生成密碼文件的工具「 htpasswd 」,若沒安裝,可直接 yum install -y httpd 進行安裝;若已安裝過了,直接使用htpasswd

生成第一個用戶

生成第二個用戶,不用加-c,若是加上會重置第一次

檢查配置nginx文件是否存在語法錯誤,並從新加載

測試訪問

提示錯誤碼401,須要用curl指定用戶

提示404,須要新建index.html

針對訪問目錄(admin),須要認證

進入配置文件,在location / 後加上admin/ 便可,保存退出

檢查配置nginx文件是否存在語法錯誤,並從新加載

作一個測試頁面,直接訪問 test.com/admin/ 會顯示401,指定用戶名和密碼後就會顯示正常

針對URL(admin.php)

修改配置文件# vim test.com.conf

檢查配置nginx文件是否存在語法錯誤,並從新加載

作測試

Nginx域名重定向

  • 更改test.com.conf

    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;

        }

    }

  • server_name後面支持寫多個域名,這裏要和httpd的作一個對比
  • permanent爲永久重定向,狀態碼爲301,若是寫redirect則爲302

修改配置文件,以下,保存退出

server_name後面支持寫多個域名,permanent爲永久重定向,狀態碼爲301,若是寫redirect則爲302

檢查配置nginx文件是否存在語法錯誤,並從新加載

作測試,用test2.com去訪問,會顯示301

定義一個不一樣的網址

用test4.com訪問,顯示404,會去訪問默認虛擬主機

相關文章
相關標籤/搜索