Nginx安裝,默認虛擬主機以及認證和重定向
阿新 • • 發佈:2018-03-14
NginxNginx安裝
1.首先下載安裝包
[root@weixing01 src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz --2018-03-14 00:46:57-- http://nginx.org/download/nginx-1.12.2.tar.gz 正在解析主機 nginx.org (nginx.org)... 206.251.255.63, 95.211.80.227, 2606:7100:1:69::3f, ... 正在連接 nginx.org (nginx.org)|206.251.255.63|:80... 已連接。 已發出 HTTP 請求,正在等待回應... 200 OK
2.解壓縮
[root@weixing01 src]# tar zxf nginx-1.12.2.tar.gz
[root@weixing01 src]# cd nginx-1.12.2/
3.編譯
[root@weixing01 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx
4.接下來安裝:
make
make install
5.安裝完成:
[root@weixing01 nginx-1.12.2]# ls /usr/local/nginx/
conf html logs sbin
6.制作啟動腳本:
[root@weixing01 nginx-1.12.2]# vim /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=$?
7.更改權限:
[root@weixing01 nginx-1.12.2]# chmod 755 /etc/init.d/nginx
[root@weixing01 nginx-1.12.2]# chkconfig --add nginx
[root@weixing01 nginx-1.12.2]# chkconfig nginx on
8.更改配置文件:
[root@weixing01 conf]# vim nginx.conf 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;
9.啟動服務:
[root@weixing01 conf]# /etc/init.d/nginx start
Starting nginx (via systemctl): [ 確定 ]
[root@weixing01 conf]# ps aux |grep nginx
root 4772 0.0 0.0 20496 624 ? Ss 01:10 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 4773 0.0 0.3 22940 3212 ? S 01:10 0:00 nginx: worker process
nobody 4774 0.0 0.3 22940 3212 ? S 01:10 0:00 nginx: worker process
root 4776 0.0 0.0 112676 984 pts/0 R+ 01:11 0:00 grep --color=auto nginx
Nginx默認虛擬主機
1.首先修改配置文件:
[root@weixing01 conf]# vim nginx.conf
application/xml;
include vhost/*.conf;
}
~
2.創建默認主機:
[root@weixing01 conf]# mkdir vhost
[root@weixing01 conf]# cd vhost/
[root@weixing01 vhost]# ls
[root@weixing01 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;
}
3.測試並重新加載:
[root@weixing01 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@weixing01 default]# /usr/local/nginx/sbin/nginx -s reload
4.驗證:
[root@weixing01 default]# curl localhost
This is the default site
用戶認證
1.做虛擬主機配置文件:
[root@weixing01 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;
}
}
2.生成密碼文件:
[root@weixing01 vhost]# /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd weixing
New password:
Re-type new password:
Adding password for user weixing
[root@weixing01 vhost]# cat /usr/local/nginx/conf/htpasswd
weixing:$apr1$bCpQS9At$I6X83gZ8dJ2f1Z3.e1y.c1
3.測試,重新加載:
[root@weixing01 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@weixing01 vhost]# /usr/local/nginx/sbin/nginx -s reload
4.驗證:
[root@weixing01 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.2</center>
</body>
</html>
[root@weixing01 vhost]# curl -u weixing:3914 -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.2</center>
</body>
</html>
創建目錄後重新測試:
[root@weixing01 vhost]# ls /data/wwwroot/test.com
ls: 無法訪問/data/wwwroot/test.com: 沒有那個文件或目錄
[root@weixing01 vhost]# mkdir /data/wwwroot/test.com
[root@weixing01 vhost]# echo "test.com" > /data/wwwroot/test.com/index.html
[root@weixing01 vhost]# curl -u weixing:3914 -x127.0.0.1:80 test.com
test.com
Nginx域名重定向
1.修改配置文件:
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;
}
location /
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
2.測試:
[root@weixing01 vhost]# curl -x127.0.0.1:80 test2.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Tue, 13 Mar 2018 17:53:50 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html
Nginx安裝,默認虛擬主機以及認證和重定向