1. 程式人生 > >十二周二課 Nginx安裝、Nginx默認虛擬主機、Nginx用戶認證、Nginx域名重定向

十二周二課 Nginx安裝、Nginx默認虛擬主機、Nginx用戶認證、Nginx域名重定向

nginx

Nginx安裝

首先進入/usr/local/src目錄。然後下載Nginx。
wget http://nginx.org/download/nginx-1.12.1.tar.gz
然後解壓
tar zxf nginx-1.12.1.tar.gz
然後進入我們剛才解壓好的目錄進行編譯
cd nginx-1.12.1
[root@linletao-001 nginx-1.12.1]# ./configure --prefix=/usr/local/nginx
這裏沒有加編譯參數,我們可以根據實際情況,在後期編譯的時候去添加參數。
在以後我們安裝環境的時候,源碼包一定要保留,因為我們不一定需要什麽模塊,所以保留源碼包還是很必要的。
編譯完成後我們進行安裝

make && make install
安裝完後我們會得到四個目錄
[root@linletao-001 nginx-1.12.1]# ls /usr/local/nginx/
conf html logs sbin
然後我們編輯一個啟動腳本
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=$?
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
也可以去網站直接復制,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
然後我們更改一下nginx的配置文件,我們要將原來的配置文件清空。
首先我們進入nginx的配置文件目錄
cd /usr/local/nginx/conf/
然後將以前的配置文件清空

/usr/local/nginx/conf/nginx.conf(>重定向符號,單獨使用,可以吧一個文檔快速清空。)
然後編輯配置文件
vim /usr/local/nginx/conf/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;
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
{
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_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}
也可以去網站直接復制https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf
然後檢查是否有語法錯誤
[root@linletao-001 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
配置語法正確。
然後我們啟動nginx
/etc/init.d/nginx start
啟動後查看是否成功啟動
[root@linletao-001 logs]# ps aux |grep nginx
root 5213 0.0 0.0 20496 620 ? Ss 22:10 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 5214 0.0 0.3 22940 3208 ? S 22:10 0:00 nginx: worker process
nobody 5215 0.0 0.3 22940 3208 ? S 22:10 0:00 nginx: worker process
root 5219 0.0 0.0 112676 976 pts/0 R+ 22:10 0:00 grep --color=auto nginx
上面顯示有兩個worker,說明啟動成功。然後我們查看一下端口
[root@linletao-001 logs]# netstat -lntp |grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5213/nginx: master
已經成功的監聽了80端口。
然後我們看是否能解析php
首先編輯文件
vim /usr/local/nginx/html/1.php
然後輸入內容
<?php
echo "this is nginx pape.";
最後用curl命令解析
[root@linletao-001 logs]# curl localhost/1.php
this is nginx pape.[root@linletao-001 logs]#
解析成功。

Nginx默認虛擬主機

首先先改變一下nginx.conf的配置文件
vim /usr/local/nginx/conf/nginx.conf
然後將配置文件中service下面的內容全部刪掉。
這裏需要註意的是}的保留,只留最後一個。
然後再最後增加一行文字
然後進入/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;
}
保存退出
然後創建mkdir -p /data/wwwroot/default/
並在下面輸入內容
echo “This is a default site.”>/data/wwwroot/default/index.html
然後檢查語法是否錯誤
/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
然後創建瀏覽器的根目錄
mkdir -p /data/wwwroot/default/
然後我麽定義一個文件
vim index.html
輸入一個內容
This is the default site.
保存後檢查語法錯誤
root@linletao-001 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
語法沒有錯的話我們就重新加載一下
/usr/local/nginx/sbin/nginx -s reload
然後測試
[root@linletao-001 default]# curl localhost
This is the default site.
[root@linletao-001 default]# curl -x127.0.0.1:80 123.com
This is the default site.
[root@linletao-001 default]# curl -x127.0.0.1:80 bbb.com
This is the default site.
測試成功。

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; (用戶名密碼文件)
}
}
我們以後在工作時可以直接復制,但是要註意server_name和root的不同。
然後我們生成用戶名密碼文件。如果我們有apache的話我們可以直接用它的密碼生成文件,如果沒有,我們可以yum先安裝一個apaceh,然後我們再去用它。
htpasswd -c /usr/local/nginx/conf/htpasswd aming(-c是生成的意思。如果想要第二個,則不用再加-c)
這裏我們將用戶密碼發到aming這個文件中。
然後我們還是去檢車語法錯誤和從新載入
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
然後我麽做測試
[root@linletao-001 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>
這裏提示401,讓我們-u指定用戶。
[root@linletao-001 vhost]# curl -uaming:19860127 -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>
這裏提示404,是因為我們還沒有創建它的主目錄。下面我們來創建它的主目錄。
mkdir /data/wwwroot/test.com
echo “test.com”>/data/wwwroot/test.com/index.html
然後我們再訪問一遍。
[root@linletao-001 vhost]# curl -uaming:19860127 -x127.0.0.1:80 test.com
“test.com”
這樣就正常了。
還有一種情況就是我們需要訪問一個目錄時才去認證,比如我們要他訪問admin時需要認證,那我們可以這樣做
在配置文件vim /usr/local/nginx/conf/vhost/test.com.conf中的location / 後面直接接admin,location / admin直接改目錄名就可以了。
這樣在我們訪問test.com時就不用輸入密碼了。而要訪問admin時就要輸入密碼。否則會就會報401。

Nginx域名重定向

Nginx的域名重定向和httpd類似。首先我們改一下配置文件
vim /usr/local/nginx/conf/vhost/test.com.conf
然後再 server_name後面增加一些域名
server_name test.com test2.com test3.com
但是這樣一來權重就變了,為了不讓權重出現變化,那麽我們要在配置文件中增加一行配置
if ($host != ‘test.com‘ ) { rewrite ^/(.*)$ http://test.com/$1 permanent;
它的意思是如果域名不是test.com,我要做一個跳轉。
然後我們做一個測試
[root@linletao-001 vhost]# curl -x127.0.0.1:80 test2.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Tue, 24 Apr 2018 16:08:27 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html
這樣不管我們輸入什麽樣的域名,他都會跳轉到我們預先設定好的tes.com。
但是我們如果輸入一個未知域名的話,它就會訪問到我們的默認虛擬主機上

十二周二課 Nginx安裝、Nginx默認虛擬主機、Nginx用戶認證、Nginx域名重定向