學習四十四
12.7 默認虛擬主機
12.8 Nginx用戶認證
12.9 Nginx域名重定向
擴展
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
Nginx安裝
cd /usr/local/src
wget http://nginx.org/download/nginx-1.12.1.tar.gz
cd /usr/local/src/nginx-1.12.1
./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
/usr/local/nginx/sbin/nginx -t
/etc/init.d/nginx start
netstat -lntp |grep 80
測試php解析
vi /usr/local/nginx/html/1.php //加入如下內容
<?php
echo "test php scripts.";
?>
curl localhost/1.php
默認虛擬主機
vim /usr/local/nginx/conf/nginx.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
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;
}
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
學習四十四