十二週二次課javascript
12.6Nginx安裝php
12.7默認虛擬主機css
12.8Nginx用戶認證html
12.9Nginx域名重定向java
12.6Nginx安裝node
Nginx安裝linux
•cd /usr/local/srcnginx
• wget http://nginx.org/download/nginx-1.12.1.tar.gzgit
• tar zxf nginx-1.12.1.tar.gzweb
• ./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
測試php解析
• vi /usr/local/nginx/html/1.php //加入以下內容
• <?php
• echo "test php scripts.";
•?>
• curl localhost/1.php
1.切換到/usr/local/src/目錄下
[root@tianqi-01 ~]# cd /usr/local/src/
[root@tianqi-01 src]#
2.下載Nginx安裝包
[root@tianqi-01 src]# wget http://nginx.org/download/nginx-1.12.1.tar.gz
3.解壓安裝包
[root@tianqi-01 src]# tar zxvf nginx-1.12.1.tar.gz
4.切換到nginx-1.12.1目錄下
[root@tianqi-01 src]# cd nginx-1.12.1
[root@tianqi-01 nginx-1.12.1]#
5.初始化./configure --prefix=/usr/local/nginx,並檢查是否成功
[root@tianqi-01 nginx-1.12.1]# ./configure --prefix=/usr/local/nginx
[root@tianqi-01 nginx-1.12.1]# echo $?
0
[root@tianqi-01 nginx-1.12.1]#
6.編譯make && make install
[root@tianqi-01 nginx-1.12.1]# make && make install
[root@tianqi-01 nginx-1.12.1]# echo $?
0
[root@tianqi-01 nginx-1.12.1]#
7.查看nginx目錄
[root@tianqi-01 nginx-1.12.1]# ls /usr/local/nginx/
conf html logs sbin
[root@tianqi-01 nginx-1.12.1]#
8.支持-t 檢查配置文件語法錯誤
[root@tianqi-01 nginx-1.12.1]# /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@tianqi-01 nginx-1.12.1]#
9.給nginx建立啓動腳本,放在/etc/init.d/nginx,配置文件內容以下
將一些內容拷貝進去
#!/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"
RETVAL=0
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: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
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()
{
stop
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
而後保存退出
10.更改配置文件的權限
[root@tianqi-01 nginx-1.12.1]# chmod 755 /etc/init.d/nginx
[root@tianqi-01 nginx-1.12.1]#
11.將nginx加入到服務列表裏
[root@tianqi-01 nginx-1.12.1]# chkconfig --add nginx
[root@tianqi-01 nginx-1.12.1]#
12.設置開機啓動nginx服務
[root@tianqi-01 nginx-1.12.1]# chkconfig nginx on
[root@tianqi-01 nginx-1.12.1]#
13.定義配置文件,默認conf目錄下有一個nginx.conf文件,但咱們不使用它,咱們使用本身配置的
[root@tianqi-01 nginx-1.12.1]# cd /usr/local/nginx/conf/
[root@tianqi-01 conf]# ls
fastcgi.conf fastcgi_params.default mime.types nginx.conf.default uwsgi_params
fastcgi.conf.default koi-utf mime.types.default scgi_params uwsgi_params.default
fastcgi_params koi-win nginx.conf scgi_params.default win-utf
[root@tianqi-01 conf]# mv nginx.conf nginx.conf.1
[root@tianqi-01 conf]#
14.建立一個配置文件,內容以下
[root@tianqi-01 conf]# vim nginx.conf
在配置文件中添加如下內容
代碼地址爲https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf
user nobody nobody; // user定義啓動nginx的用戶
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; //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;
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 對應一個虛擬主機,定義這個,才能正常訪問網站,下面一整段,就是一個默認的虛擬主機
{
listen 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; //nginx經過這一行配置來調用php-fpm服務
# fastcgi_pass 127.0.0.1:9000; //若是php-fpm監聽的是9000端口,直接這樣寫配置便可
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}
而後保存退出
15.編譯好配置文件,檢查配置文件是否存在語法錯誤
[root@tianqi-01 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@tianqi-01 conf]#
16.啓動nginx服務
[root@tianqi-01 conf]# /etc/init.d/nginx start
Starting nginx (via systemctl): [ OK ]
[root@tianqi-01 conf]#
17.查看nginx進程
[root@tianqi-01 conf]# ps aux |grep nginx
root 3701 0.0 0.0 20496 624 ? Ss 17:32 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/confnginx.conf
nobody 3702 0.0 0.3 22940 3208 ? S 17:32 0:00 nginx: worker process
nobody 3703 0.0 0.3 22940 3208 ? S 17:32 0:00 nginx: worker process
root 3705 0.0 0.0 112660 984 pts/0 R+ 17:32 0:00 grep --color=auto nginx
[root@tianqi-01 src]# ps aux |grep php-fpm
root 1009 0.0 0.4 227204 4952 ? Ss 15:57 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm 1010 0.0 0.4 227204 4712 ? S 15:57 0:00 php-fpm: pool www
php-fpm 1011 0.0 0.4 227204 4712 ? S 15:57 0:00 php-fpm: pool www
php-fpm 1012 0.0 0.4 227204 4712 ? S 15:57 0:00 php-fpm: pool www
php-fpm 1013 0.0 0.4 227204 4712 ? S 15:57 0:00 php-fpm: pool www
php-fpm 1014 0.0 0.4 227204 4716 ? S 15:57 0:00 php-fpm: pool www
php-fpm 1015 0.0 0.5 227204 5724 ? S 15:57 0:00 php-fpm: pool www
php-fpm 1016 0.0 0.4 227204 4716 ? S 15:57 0:00 php-fpm: pool www
php-fpm 1017 0.0 0.4 227204 4716 ? S 15:57 0:00 php-fpm: pool www
php-fpm 1018 0.0 0.4 227204 4716 ? S 15:57 0:00 php-fpm: pool www
php-fpm 1019 0.0 0.4 227204 4716 ? S 15:57 0:00 php-fpm: pool www
php-fpm 1020 0.0 0.4 227204 4716 ? S 15:57 0:00 php-fpm: pool www
php-fpm 1021 0.0 0.4 227204 4716 ? S 15:57 0:00 php-fpm: pool www
php-fpm 1022 0.0 0.4 227204 4716 ? S 15:57 0:00 php-fpm: pool www
php-fpm 1023 0.0 0.4 227204 4720 ? S 15:57 0:00 php-fpm: pool www
php-fpm 1024 0.0 0.4 227204 4720 ? S 15:57 0:00 php-fpm: pool www
php-fpm 1025 0.0 0.4 227204 4720 ? S 15:57 0:00 php-fpm: pool www
php-fpm 1026 0.0 0.4 227204 4720 ? S 15:57 0:00 php-fpm: pool www
php-fpm 1027 0.0 0.4 227204 4720 ? S 15:57 0:00 php-fpm: pool www
php-fpm 1028 0.0 0.4 227204 4720 ? S 15:57 0:00 php-fpm: pool www
php-fpm 1029 0.0 0.4 227204 4720 ? S 15:57 0:00 php-fpm: pool www
root 3791 0.0 0.0 112660 984 pts/0 R+ 19:12 0:00 grep --color=auto php-fpm
[root@tianqi-01 src]#
18.測試nginx,這裏能夠輸入curl localhost 或者輸入curl 127.0.0.1 獲得的結果相同
[root@tianqi-01 conf]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title> //nginx歡迎頁面
<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@tianqi-01 conf]# ls /usr/local/nginx/html/
50x.html index.html
[root@tianqi-01 conf]# curl 127.0.0.1
<!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@tianqi-01 src]#
19.測試解析php,新建一個1.php文件
[root@tianqi-01 conf]# vim /usr/local/nginx/html/1.php
<?php
echo "This is nginx test page.";
?>
保存退出
[root@tianqi-01 conf]# curl localhost/1.php
This is nginx test page.[root@tianqi-01 conf]# //說明解析成功
12.7默認虛擬主機
•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
1.首先刪除/usr/local/nginx/conf/nginx.conf 中的一部份內容——>目的是修改nginx.cnf配置,刪除默認的虛擬主機配置,從新定義虛擬主機配置所在路徑
[root@tianqi-01 conf]# vim /usr/local/nginx/conf/nginx.conf
刪除的內容
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;
}
}
2.而後在配置文件中增長一行,include vhost/*.conf
application/xml;
include vhost/*.conf;
而後保存退出
3.新建/usr/local/nginx/conf/vhost目錄
[root@tianqi-01 conf]# pwd
/usr/local/nginx/conf
[root@tianqi-01 conf]# mkdir vhost
[root@tianqi-01 conf]#
4.進入到/usr/local/nginx/conf/vhost目錄下
[root@tianqi-01 conf]# cd vhost
[root@tianqi-01 vhost]#
5.定義新增虛擬主機的配置
[root@tianqi-01 vhost]# vim aaa.com.conf
添加的文件內容
server
{
listen 80 default_server; //有這個標記就是默認虛擬主機
server_name aaa.com;
index index.html index.htm index.php;//指定索引頁
root /data/wwwroot/default;
}
而後保存退出
6.建立目錄
[root@tianqi-01 vhost]# mkdir /data/wwwroot/default/
[root@tianqi-01 vhost]#
7.切換到/data/wwwroot/default/目錄下,在目錄下寫入一些東西
[root@tianqi-01 vhost]# cd /data/wwwroot/default/
[root@tianqi-01 default]#
8.新建index.html,寫入一些東西
[root@tianqi-01 default]# vim index.html
寫入如下內容
This is the default site.
保存退出
9.檢測配置文件是否存在語法錯誤
[root@tianqi-01 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@tianqi-01 default]#
下面故意寫錯,查看錯誤提示
[root@tianqi-01 default]# vim /usr/local/nginx/conf/vhost/aaa.com.conf
server
{
listen 80 default_server;
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
afagstyasf //寫入亂碼
}
保存退出
檢查語法錯誤
[root@tianqi-01 default]# /usr/local/nginx/sbin/nginx -t
nginx: [emerg] unexpected "}" in /usr/local/nginx/conf/vhost/aaa.com.conf:8
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
[root@tianqi-01 default]#
再改回來
[root@tianqi-01 default]# vim /usr/local/nginx/conf/vhost/aaa.com.conf
[root@tianqi-01 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@tianqi-01 default]#
11.在修改配置文件後,通常都 -t 去檢查下,防止誤操做
12.修改完,重啓nginx或者從新加載nginx
[root@tianqi-01 default]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 default]#
13.測試訪問默認頁
[root@tianqi-01 default]# curl localhost
This is the default site.
[root@tianqi-01 default]# curl -x127.0.0.1:80 bbb.com
This is the default site.
[root@tianqi-01 default]# curl -x127.0.0.1:80 aaa.com
This is the default site.
[root@tianqi-01 default]#
//默認是aaa.com,無論訪問什麼域名,默認虛擬主機就是這樣,無論什麼域名,只要解析過來,只想到咱們的服務器,它都能訪問到這個站點。
14.nginx支持include這種語法
由於修改了nginx.conf的配置,如今看到的默認索引頁,是咱們剛剛新增的vhost的虛擬主機的索引頁了 定義默認虛擬主機的兩種辦法: 1.默認虛擬主機,是根據目錄的第一個.conf了進行選擇,因此只須要在vhost目錄下依次建立就能夠了,固然這種方法不智能 2.只須要在vhost目錄的.conf配置文件內,加上一個「default_server 」便可,把當前的這個配置對應的網站設置爲第一個默認虛擬主機。
12.8Nginx用戶認證
•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;
}
1.首先切換到usr/local/nginx/conf/vhost/目錄下
[root@tianqi-01 default]# cd /usr/local/nginx/conf/vhost/
[root@tianqi-01 vhost]#
2.新建新建一個虛擬主機test.com.conf,並編輯
[root@tianqi-01 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 / //表示全站,都須要進行用戶認證
#location /admin // 這個地方只要加上」 /admin 」 就變成 針對這個站點的「admin」 這個目錄須要用戶認證
#location ~ admin.php //若是把這行這樣寫,就會變成,匹配 「 admin.php 」這個頁面的時候才須要用戶認證
{
auth_basic //定義用戶認證的名字
auth_basic_user_file /usr/local/nginx/conf/htpasswd; //用戶名密碼文件
}
}
保存退出
3.在配置完成後,須要生成密碼文件
4.在生成密碼文件,須要用到Apache生成密碼文件的工具「 htpasswd 」
/usr/local/apache2.4/bin/htpasswd
yum install -y httpd
5.這裏因爲未安裝過Apache,因此先yum安裝
[root@tianqi-01 vhost]# yum install -y httpd
6.htpasswd指定文件,生成用戶
[root@tianqi-01 vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd tianqi
New password: //密碼123456
Re-type new password:
Adding password for user tianqi
[root@tianqi-01 vhost]#
7.使用cat 命令查看/usr/local/nginx/conf/htpasswd 文件,會看到生成了一行字符串
[root@tianqi-01 vhost]# cat /usr/local/nginx/conf/htpasswd
tianqi:$apr1$MO1ysyDK$r0Eq.JoAuWRSVzXBYY1Xq0
[root@tianqi-01 vhost]#
8.關於htpasswd -c 命令 第一次建立的時候由於沒有htpasswd這個文件,須要-c建立,第二使用的時候由於已經有這個htpasswd文件了,將再也不須要-c 選項,若是還繼續使用-c 這個選項,將會重置 htpasswd裏的東西
9.再來htpasswd指定文件,生成另外一個用戶
[root@tianqi-01 vhost]# /usr/local/apache2.4/bin/htpasswd /usr/local/nginx/conf/htpasswd user1
New password: //密碼爲123456
Re-type new password:
Adding password for user user1
[root@tianqi-01 vhost]# cat /usr/local/nginx/conf/htpasswd
tianqi:$apr1$MO1ysyDK$r0Eq.JoAuWRSVzXBYY1Xq0
user1:$apr1$ckJMbZrV$J1hEZ3dgpg7mwXe0nf/hD1
[root@tianqi-01 vhost]#
10.檢查配置nginx文件是否存在語法錯誤
[root@tianqi-01 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
11.從新加載配置文件
[root@tianqi-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 vhost]#
12.測試
[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test.com
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test.com -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.12.1
Date: Mon, 12 Mar 2018 12:55:19 GMT
Content-Type: text/html
Content-Length: 195
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"
[root@tianqi-01 vhost]#
13.會提示錯誤碼401,就是須要用戶,因此用curl指定用戶
14.這時指定用戶和密碼再來訪問,會提示404,這是由於去訪問index.html,可是還未建立
[root@tianqi-01 vhost]# curl -utianqi:123456 -x127.0.0.1:80 test.com
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@tianqi-01 vhost]#
15.建立目錄,而後新建index.html
[root@tianqi-01 vhost]# mkdir /data/wwwroot/test.com
[root@tianqi-01 vhost]# echo "test.com" > /data/wwwroot/test.com/index.html
[root@tianqi-01 vhost]#
16.這時再來訪問,會看到顯示正常
[root@tianqi-01 vhost]# curl -utianqi:123456 -x127.0.0.1:80 test.com
test.com
[root@tianqi-01 vhost]#
17.這裏的用戶認證是針對整站
1.首先訪問admin嘗試下
[root@tianqi-01 vhost]# curl -utianqi:123456 -x127.0.0.1:80 test.com/admin/
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@tianqi-01 vhost]#
2.而後在/usr/local/nginx/conf/vhost/test.com.conf配置文件中定義,只須要在location / 後加上admin/ 目錄便可
[root@tianqi-01 vhost]# vim test.com.conf
//在location後面添加 / admin /便可
server
{
listen 80;
server_name test.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
location /admin/
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
3.檢查配置文件是否存在語法錯誤
[root@tianqi-01 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@tianqi-01 vhost]#
4.從新加載配置文件
[root@tianqi-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 vhost]#
5.這時候再來訪問test.com,就不須要指定用戶名和密碼了
[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test.com
test.com
[root@tianqi-01 vhost]#
6.訪問test.com/admin/目錄
[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test.com/admin/
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@tianqi-01 vhost]#
7.這時建立一個測試頁面
8.先新建目錄
[root@tianqi-01 vhost]# mkdir /data/wwwroot/test.com/admin
[root@tianqi-01 vhost]#
9.而後在admin目錄下新建index.html
[root@tianqi-01 vhost]# echo "test.com admin dir" > /data/wwwroot/test.com/admin/index.html
[root@tianqi-01 vhost]#
10.這時再來訪問 test.com/admin/ 會顯示401,可是指定用戶名和密碼後就會正常顯示
[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test.com/admin/
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@tianqi-01 vhost]# curl -utianqi:123456 -x127.0.0.1:80 test.com/admin/
test.com admin dir
[root@tianqi-01 vhost]#
1.首先在配置文件/usr/local/nginx/conf/vhost/test.com.conf下定義,在 location 後加~ admin.php便可
[root@tianqi-01 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 ~admin.php //在location後面加~admin.php
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
保存退出
2.檢查配置文件是否存在語法錯誤
[root@tianqi-01 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@tianqi-01 vhost]#
3.從新加載配置文件
[root@tianqi-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 vhost]#
4.這時候就能夠直接訪問 test.com/admin/,不須要指定用戶名和密碼了,可是在訪問admin.php的時候,則會顯示401——>狀態碼爲401說明須要驗證
[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test.com/admin/
test.com admin dir
[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test.com/admin.php
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@tianqi-01 vhost]#
12.9Nginx域名重定向
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
1.修改配置文件vim /usr/local/nginx/conf/vhost/test.com.conf,(這裏刪除用戶認證那一塊代碼)
[root@tianqi-01 vhost]# !vim
vim test.com.conf
server
{
listen 80;
server_name test.com test2.com test3.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
}
2.檢查配置文件語法錯誤,並從新加載配置文件
[root@tianqi-01 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@tianqi-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 vhost]#
3.測試,用test2.com去訪問,會看到顯示301,給它重定向到了http://test.com/index.html
[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test2.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Mon, 12 Mar 2018 14:00:50 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html
[root@tianqi-01 vhost]#
4.定義一個不一樣的網址再來訪問
[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test2.com/dagrfe -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Mon, 12 Mar 2018 14:01:56 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/dagrfe
[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test3.com/dagrfe -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Mon, 12 Mar 2018 14:04:37 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/dagrfe
[root@tianqi-01 vhost]#
5.它會訪問默認虛擬主機
6.這時如果隨意訪問一個不存在的網址,則會顯示404
[root@tianqi-01 vhost]# curl -x127.0.0.1:80 tianqi.com/admin/index.html -I
HTTP/1.1 404 Not Found
Server: nginx/1.12.1
Date: Mon, 12 Mar 2018 14:03:29 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
[root@tianqi-01 vhost]#
友情連接:阿銘Linux
友情連接:nginx.conf 配置詳解1
友情連接:nginx.conf 配置詳解2
友情連接:nginx rewrite四種flag