首先切換到/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測試文件
解析成功
server
{
listen 80 default_server; // 有這個標記的就是默認虛擬主機
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
}
首先切換到usr/local/nginx/conf目錄下#cd /usr/local/nginx/conf
編輯Nginx配置文件
刪除原有server內容
在刪除內容的原有位置添加一行include vhost/*.conf;保存退出
添加一臺虛擬主機
建立目錄,在目錄下新建index.html
檢測配置文件是否存在語法錯誤
從新加載nginx
測試訪問
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;
}
}
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
進入配置文件,在location / 後加上admin/ 便可,保存退出
檢查配置nginx文件是否存在語法錯誤,並從新加載
作一個測試頁面,直接訪問 test.com/admin/ 會顯示401,指定用戶名和密碼後就會顯示正常
修改配置文件# vim test.com.conf
檢查配置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;
}
}
修改配置文件,以下,保存退出
server_name後面支持寫多個域名,permanent爲永久重定向,狀態碼爲301,若是寫redirect則爲302
檢查配置nginx文件是否存在語法錯誤,並從新加載
作測試,用test2.com去訪問,會顯示301
定義一個不一樣的網址
用test4.com訪問,顯示404,會去訪問默認虛擬主機