47.Nginx安裝 預設虛擬主機 使用者認證 域名重定向
12.6 Nginx安裝
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
12.6 Nginx安裝:
~1. cd /usr/local/src
~2.wget http://nginx.org/download/nginx-1.8.0.tar.gz
~3.tar zxf nginx-1.8.0.tar.gz
~4.cd /usr/local/nginx-1.8.0.tar.gz
~5../configure --prefix=/usr/local/nginx (此處看需求需要哪種模組就要加上。後期會用到https,再來重新編譯)
~6.make && make install
~7. vim /etc/init.d/nginx 建立啟動指令碼 複製例項中內容
(參考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx )
~8.chmod 755 /etc/init.d/nginx
~9.chkconfig --add nginx
~10.chkconfig nginx on
~11.cd /usr/local/nginx/conf/; mv nginx.conf nginx.conf.bak 配置檔案
~12.vim nginx.conf 複製例項中內容
(參考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf)
~13./usr/local/nginx/sbin/nginx -t
~14./etc/init.d/nginx start
~15. netstat -lntp |grep 80
測試php解析:
vi /usr/local/nginx/html/1.php 加入如下內容
<?php
echo "test php scripts.";
?>
curl localhost/1.php
例項:
[root@localhost ~]# cd /usr/local/src
[root@localhost src]# wget http://nginx.org/download/nginx-1.8.0.tar.gz
[root@localhost src]# tar -zxvf nginx-1.8.0.tar.gz
[root@localhostsrc]# cd nginx-1.8.0/
[root@localhost nginx-1.8.0]# ./configure --prefix=/usr/local/nginx
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ md5: using system crypto library
+ sha1: using system crypto library
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
[root@localhost nginx-1.8.0]# echo $?
0
[root@localhost nginx-1.8.0]# make
make[1]: 離開目錄“/usr/local/src/nginx-1.8.0”
make -f objs/Makefile manpage
make[1]: 進入目錄“/usr/local/src/nginx-1.8.0”
sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
-e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \
-e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \
-e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \
< man/nginx.8 > objs/nginx.8
make[1]: 離開目錄“/usr/local/src/nginx-1.8.0”
[root@localhost nginx-1.8.0]# echo $?
0
[root@localhost nginx-1.8.0]# make install
make -f objs/Makefile install
make[1]: 進入目錄“/usr/local/src/nginx-1.8.0”
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
test -d '/usr/local/nginx/sbin' || mkdir -p '/usr/local/nginx/sbin'
test ! -f '/usr/local/nginx/sbin/nginx' || mv '/usr/local/nginx/sbin/nginx' '/usr/local/nginx/sbin/nginx.old'
cp objs/nginx '/usr/local/nginx/sbin/nginx'
test -d '/usr/local/nginx/conf' || mkdir -p '/usr/local/nginx/conf'
cp conf/koi-win '/usr/local/nginx/conf'
cp conf/koi-utf '/usr/local/nginx/conf'
cp conf/win-utf '/usr/local/nginx/conf'
test -f '/usr/local/nginx/conf/mime.types' || cp conf/mime.types '/usr/local/nginx/conf'
cp conf/mime.types '/usr/local/nginx/conf/mime.types.default'
test -f '/usr/local/nginx/conf/fastcgi_params' || cp conf/fastcgi_params '/usr/local/nginx/conf'
cp conf/fastcgi_params '/usr/local/nginx/conf/fastcgi_params.default'
test -f '/usr/local/nginx/conf/fastcgi.conf' || cp conf/fastcgi.conf '/usr/local/nginx/conf'
cp conf/fastcgi.conf '/usr/local/nginx/conf/fastcgi.conf.default'
test -f '/usr/local/nginx/conf/uwsgi_params' || cp conf/uwsgi_params '/usr/local/nginx/conf'
cp conf/uwsgi_params '/usr/local/nginx/conf/uwsgi_params.default'
test -f '/usr/local/nginx/conf/scgi_params' || cp conf/scgi_params '/usr/local/nginx/conf'
cp conf/scgi_params '/usr/local/nginx/conf/scgi_params.default'
test -f '/usr/local/nginx/conf/nginx.conf' || cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf'
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/logs' || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs' || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' || cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' || mkdir -p '/usr/local/nginx/logs'
make[1]: 離開目錄“/usr/local/src/nginx-1.8.0”
[root@localhost nginx-1.8.0]# echo $?
0
[root@localhost nginx-1.8.0]# ls /usr/local/nginx/ 看下他的目錄,很小,沒有多少檔案
conf html logs sbin
[root@localhost nginx-1.8.0]# ls /usr/local/nginx/conf conf下就是配置檔案
conf:
fastcgi.conf fastcgi_params koi-utf koi-win mime.types nginx.conf scgi_params uwsgi_params win-utf
/usr/local/nginx/conf:
fastcgi.conf fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utf
fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default
[root@localhost nginx-1.8.0]# ls /usr/local/nginx/conf conf
html:
50x.html index.html
/usr/local/nginx/html:
50x.html index.html
下就是樣例(index.html)
50x.html index.html
[root@localhost nginx-1.8.0]# ls /usr/local/nginx/logs /logs下就是存放日誌的
[root@localhost nginx-1.8.0]# ls /usr/local/nginx/sbin /sbin下就是他的核心檔案
nginx
[root@localhost nginx-1.8.0]# /usr/local/nginx/sbin/nginx -t /-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@localhost nginx-1.8.0]# vim /etc/init.d/nginx 啟動指令碼中複製一下內容
#!/bin/bash
#!/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"
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: "
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()
{
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@localhost nginx-1.8.0]# chmod 755 /etc/init.d/nginx
[root@localhost nginx-1.8.0]# chkconfig --add nginx
[root@localhost nginx-1.8.0]# chkconfig nginx on
[root@localhostconf]# cd /usr/local/nginx/conf/
[root@localhost conf]# ls 其實裡面有一個.conf的配置檔案,但是我們不用這個。重新設定一個
fastcgi.conf fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utf
fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default
[root@localhost conf]# mv nginx.conf nginx.conf.1 把自帶的改個名字
[root@localhost conf]# vim nginx.conf 在建立一個我們需要的,直接vim就可以了。
複製一下的:
user nobody nobody; 用來定義nginx的啟動是哪個使用者。其實就是這個程序的使用者,比如去一個目錄下讀一個圖片,那麼是有哪個使用者的身份去讀的呢?就在這定義
worker_processes 2; 定義子程序有幾個
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; 使用epoll模式
worker_connections 6000; 程序最多有多少個連線
}
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對應的一個虛擬主機。也就是預設的虛擬主機。跟Apache的VirtualHost類似後面也可以再繼續加
{
listen 80; 監聽80埠
server_name localhost; 域名
index index.html index.htm index.php;
root /usr/local/nginx/html; 網站的根目錄
location ~ \.php$ 用來解析php的部分
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock; 我們用的是sock,所以這樣寫
#fastcgi_pass 127.0.0.1:9000 如果監聽的是指定的。可以這樣寫你需要指定的IP就可以了
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 6000;
}
http
{
include mime.types;
default_type application/octet-stream;
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;
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_comp_level 5;
gzip_types text/plain application/x-javascript text/css text/htm
gzip_comp_level 5;
gzip_types text/plain application/x-javascript text/css text/htm
server
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ \.php$
{
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;
}
}
}
[root@localhost 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@localhost conf]# /etc/init.d/nginx start
Starting nginx (via systemctl): [ 確定 ]
[root@localhost conf]# ps aux |grep nginx
root(父程序一般是root) 4936 0.0 0.0 24880 788 ? Ss 00:11 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 4937 0.0 0.1 27324 3364 ? S 00:11 0:00 nginx: worker process
nobody 4938 0.0 0.1 27324 3364 ? S 00:11 0:00 nginx: worker process
root 4940 0.0 0.0 112720 980 pts/0 S+ 00:11 0:00 grep --color=auto nginx
root 3775 0.0 0.0 24884 788 ? Ss 13:29 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 3776 0.0 0.3 27328 3352 ? S 13:29 0:00 nginx: worker process
nobody 3777 0.0 0.3 27328 3608 ? S 13:29 0:00 nginx: worker process
root 3779 0.0 0.0 112724 988 pts/0 R+ 13:30 0:00 grep --color=auto nginx
[root@localhost conf]# vim /usr/local/nginx/html/1.php 建一個php測試一下
<?php
echo "test php scripts.";
?>
[root@localhost conf]# curl localhost/1.php 我們在配置檔案了設定了localhost的目錄就是/usr/local/nginx/html,所以這裡直接寫localhost/1.php就可以了
test php scripts.[root@localhost conf]#
# 這就說明解析成功了
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12.7 預設虛擬主機:
上一節我們定義了預設虛擬主機配置檔案,其實就是第一個。當然,以下是專門來定義預設虛擬主機的配置段:
~1.vim /usr/local/nginx/conf/nginx.conf //增加:(之間配置了service的,要先把service那些刪掉,在增加)
include vhost/*.conf
~2.mkdir /usr/local/nginx/conf/vhost
~3.cd !$; vim aaa.com.conf //加入如下內容
server
{
listen 80 default_server; // 有default_service這個標記的就是預設虛擬主機
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
}
~4.mkdir -p /data/wwwroot/default/
~5. echo “This is a default site.”>/data/wwwroot/default/index.html
~6./usr/local/nginx/sbin/nginx -t
~7./usr/local/nginx/sbin/nginx -s reload
~8.curl localhost
~9.curl -x127.0.0.1:80 123.com
例項:
[root@localhostconf]# vim /usr/local/nginx/conf/nginx.conf
http http包含的service,在最下面定義一下include
{
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm
application/xml;
include vhost/*.conf;
[root@localhost conf]# pwd
/usr/local/nginx/conf 定義的clude就是在這個目錄下的
[root@localhost conf]# mkdir vhost 建立上面定義include的目錄。就是在conf目錄下的
[root@localhost conf]# vim aaa.com.conf 建立一個比如叫aaa.com.conf的檔案。注意字尾名,我們定義了vhost下的所有.conf的檔案,所以要加上.conf。複製上下面的:
erver
{
listen 80 default_server; 有這個的就是預設的虛擬主機
server_name aaa.com; 名字叫aaa.com
index index.html index.htm index.php; 指定索引頁
root /data/wwwroot/default; 指定網站根目錄
}
server
{
listen 80 default_server;
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
}
以上指定了網站的根目錄,還要建立/data/wwwroot/default的這個目錄
[root@localhost conf]# mkdir /data/wwwroot/default 建立default這個目錄
[root@localhost conf]# cd /data/wwwroot/default cd到這個目錄
[root@localhost default]# vim index.html vim一個index.html的檔案
[root@localhost 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@localhost default]# /usr/local/nginx/sbin/nginx -s reload 重新載入
[root@localhost vhost]# curl localhost
“This is a default site.” 不顯示原因不明
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost vhost]# curl -x192.168.30.134:80 bbb.com
“This is a default site.” 不管訪問的是什麼,都是顯示的這個。這個就叫預設虛擬主機
阿鑫在做的時候curl報錯拒絕連結,發現沒有在vhost的目錄下vim aaa.com.conf。需注意
我們在vhost裡建立了aaa.com.conf裡面設定了default_service這個預設虛擬主機的標記。其實在vhost這個目錄下可以建立很多.conf的檔案(也就是虛擬主機),他也可以按順序排列,放在第一位的就是預設虛擬主機。當然也可以通過名字,比如,aaa bbb ccc,那麼aaa就是預設虛擬主機,當然這個也不是一個好的方法
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12.8 Nginx使用者認證:
需要定義一個使用者密碼認證的檔案:
~1.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; 使用者名稱密碼檔案
}
}
~2.yum install -y httpd 安裝httpd,為的是生成密碼檔案。要使用htpasswd這個命令
~3.htpasswd -c /usr/local/nginx/conf/htpasswd axin
~4.-t && -s reload //測試配置並重新載入
~5.mkdir /data/wwwroot/test.com
~6.echo “test.com”>/data/wwwroot/test.com/index.html
~7.curl -x127.0.0.1:80 test.com -I//狀態碼為401說明需要驗證
~8.curl -uaming:passwd 訪問狀態碼變為200
~9. 編輯windows的hosts檔案,然後在瀏覽器中訪問test.com會有輸入使用者、密碼的彈窗
~~1.針對目錄的使用者認證(這個網站下的某一個目錄,例如admin目錄)。
vim /usr/local/nginx/conf/vhost/test.com.conf 的location /後面加上admin目錄就可以了
location /admin/
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
~~2. 訪問admin下的php檔案(單獨一個檔案)的時候做使用者驗證
vim /usr/local/nginx/conf/vhost/test.com.conf 的location後面加上 ~admin.php
location ~ admin.php
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
例項:
[root@localhost ~]# cd /usr/local/nginx/conf/vhost/
[root@localhost vhost]# ls
aaa.com.conf
[root@localhost vhost]# vim 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; 使用者名稱密碼檔案
}
}
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;
}
}
[root@localhost vhost]# yum install -y httpd
[root@localhost vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd axin /-c代表生成密碼檔案和使用者
New password:
Re-type new password:
Adding password for user axin
[root@localhost vhost]# htpasswd /usr/local/nginx/conf/htpasswd user1 不加-c是生成另外一個。加了-c會覆蓋之前的使用者
New password:
Re-type new password:
Adding password for user user1
[root@localhost vhost]# cat /usr/local/nginx/conf/htpasswd 這就是我們生成的兩個使用者
axin:$apr1$65xVjybI$7J87ZKPixirDwtV6yISLz.
user1:$apr1$eNxB0qYf$L6O9tM5kb82fdvPk1UOQM1
[root@localhost vhost]# /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@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload 重新載入(如果配置裡有錯的話,是不會生效的。不會破壞原來nginx的服務。如果直接restart的話,萬一配置檔案裡有錯誤,一重啟會導致服務停掉)最好是使用reload
[root@localhost vhost]# curl -x127.0.0.1:80 test.com 測試為401(使用者認證,沒有許可權)
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.8.0</center>
</body>
</html>
[root@localhost vhost]# curl -uaxin:wangxin789 -x127.0.0.1:80 test.com 404(因為我們還沒有建立他的根目錄)
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.8.0</center>
</body>
</html>
[root@localhost vhost]# ls /data/wwwroot/ 看一下沒有建立test.com
111.com 123.php abc.com default
[root@localhost vhost]# mkdir /data/wwwroot/test.com 建立test.com的目錄
[root@localhost vhost]# echo "wozhenniu" > /data/wwwroot/test.com/index.html 給他建立一個index.html的檔案,並輸入點字元
[root@localhost vhost]# curl -uaxin:wangxin789 -x127.0.0.1:80 test.com 測試成功
wozhenniu
已上是針對整個網站做使用者認證,下面是針對一個目錄下做使用者認證:
~~1.
[root@localhost vhost]# vim /usr/local/nginx/conf/vhost/test.com.conf 開啟.conf
server
{
listen 80;
server_name test.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
location /admin/ 加入admin/
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
[root@localhost vhost]# /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@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost vhost]# mkdir /data/wwwroot/test.com/admin/
[root@localhost vhost]# echo "wozhenni" > /data/wwwroot/test.com/admin/index.html
[root@localhost vhost]# curl -x127.0.0.1:80 test.com/admin/index.html -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.8.0
Date: Tue, 23 Jul 2019 06:24:23 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"
[root@localhost vhost]# curl -uaxin:123456 -x127.0.0.1:80 test.com/admin/index.html -I
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Tue, 23 Jul 2019 06:25:01 GMT
Content-Type: text/html
Content-Length: 9
Last-Modified: Tue, 23 Jul 2019 06:24:06 GMT
Connection: keep-alive
ETag: "5d36a806-9"
Accept-Ranges: bytes
[root@localhost 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 ~ admin.php 指定單獨的.php檔案
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
[root@localhost vhost]# vim /data/wwwroot/test.com/admin.php 建立這個admin.php
[root@localhost vhost]# /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@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost vhost]# curl -x127.0.0.1:80 test.com/admin.php -I 測試顯示401
HTTP/1.1 401 Unauthorized
Server: nginx/1.8.0
Date: Tue, 23 Jul 2019 06:31:07 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"
[root@localhost vhost]# curl -uaxin:123456 -x127.0.0.1:80 test.com/admin.php -I 指定使用者200
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Tue, 23 Jul 2019 06:32:01 GMT
Content-Type: application/octet-stream
Content-Length: 0
Last-Modified: Tue, 23 Jul 2019 06:27:11 GMT
Connection: keep-alive
ETag: "5d36a8bf-0"
Accept-Ranges: bytes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12.9 Nginx域名重定向:
~1.更改test.com.conf(vim /usr/local/nginx/conf/vhost/test.com.conf)
~2.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' ) { 跟apache的域名跳轉是一樣的
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
}
~3.server_name後面支援寫多個域名,這裡要和httpd的做一個對比
~4.permanent為永久重定向,狀態碼為301。如果寫redirect則為302。暫時重定向
例項:
[root@localhost vhost]# vim /usr/local/nginx/conf/vhost/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;
}
}
[root@localhost vhost]# /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@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost vhost]# curl -x127.0.0.1:80 test1.com -I 301,跳轉到test.com
HTTP/1.1 301 Moved Permanently
Server: nginx/1.8.0
Date: Tue, 23 Jul 2019 06:40:42 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/