Nginx 安裝
-
下載並解壓包
下載Nginx:
[root@localhost src]# wget http://nginx.org/download/nginx-1.8.0.tar.gz解壓Nginx
[root@localhost src]# tar -zxv -f nginx-1.8.0.tar.gz -
配置Nginx:
切換目錄:
[root@localhost src]# cd nginx-1.8.0配置:
[root@localhost nginx-1.8.0]# ./configure --prefix=/usr/local/nginx
#如果需要支持某模塊,可以在此添加,如HTTPS、SSL等
......
[root@localhost nginx-1.8.0]# echo $?
0編譯和安裝:
[root@localhost nginx-1.8.0]# make
[root@localhost nginx-1.8.0]# echo $?
0
[root@localhost nginx-1.8.0]# make install
[root@localhost nginx-1.8.0]# echo $?
0查看安裝目錄:
[root@localhost nginx-1.8.0]# ls /usr/local/nginx/
conf html logs sbin -
創建啓動腳本
在/etc/init.d/目錄下創建nginx文件,並添加如下內容:
[root@localhost nginx-1.8.0]# 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更改權限:
[root@localhost nginx-1.8.0]# chmod 755 /etc/init.d/nginx設置nginx開機啓動
[root@localhost nginx-1.8.0]# chkconfig --add nginx
[root@localhost nginx-1.8.0]# chkconfig nginx on
[root@localhost nginx-1.8.0]# -
更改配置文件
[root@localhost nginx-1.8.0]# cd /usr/local/nginx/conf/
[root@localhost conf]# mv nginx.conf nginx.conf.bak
[root@localhost conf]# vim nginx.confuser 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;
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;
#php文件路徑
location ~ .php$
#配置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;
}
}
}檢查配置文件是否有錯:
[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啓動nginx服務:
[root@localhost conf]# /etc/init.d/nginx start
Starting nginx (via systemctl): [ 確定 ]測試nginx解析php:
[root@localhost conf]# vim usr/local/nginx/html/1.php<?php
echo "this is nginx test page";[root@localhost conf]# curl localhost/1.php
this is nginx test page
Nginx 默認虛擬主機
編輯nginx配置文件:
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
......
將server部分去掉。
添加下面這行:
include vhost/*.conf;
#創建一個虛擬主機配置文件子目錄(相當於增加子虛擬主機)
創建vhost目錄:
[root@localhost conf]# mkdir vhost
在vhost目錄下創建一臺虛擬主機:
[root@localhost vhost]# vim aaa.com.conf
server
{
listen 80 default_server;
#有'default_server'標記的就是默認虛擬主機
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
}
創建配置文件中root指定的目錄
[root@localhost vhost]# mkdir -p /data/wwwroot/default
添加索引頁:
[root@localhost vhost]# vim /data/wwwroot/default/index.html
This is the default directory.
檢測配置文件:
[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服務。
檢測:
[root@localhost vhost]# curl localhost
This is the default directory.
#添加一臺虛擬主機,所謂默認虛擬主機就是/usr/local/nginx/conf/vhost目錄下虛擬主機配置文件中有“default_server”標記的虛擬主機。
Nginx 用戶認證
-
創建一臺虛擬主機
[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;
#指定用戶的密碼文件
}
}
注意: 上述“location”中的內容即爲設定用戶認證。在此是爲整個站點設定的用戶認證,如果只是爲某個目錄設置用戶認證,在location所在行進行編輯就好,如:location /admin 目錄。也可以對某種請求(即對一個普通文件)設定用戶認證,如location ~ admin.php()使用 ~ 進行匹配)
-
創建密碼文件:
在此需要使用Apache的/usr/local/apache/bin/htpasswd命令,如果機器中已經有Apache,可以直接使用,如果沒有,需要使用yum安裝httpd命令。
安裝httpd:
[root@localhost vhost]# yum install -y httpd創建密碼文件:
[root@localhost vhost]# htpasswd -c -m /usr/local/nginx/conf/htpasswd huang
New password:
Re-type new password:
Adding password for user huang創建密碼文件htpasswd,指定用戶爲黃。‘-c’=create,創建該密碼文件,如果是第二次添加用戶,不用加該選項,所添加的用戶名和密碼會保存到該文件下。-m 使用md5加密文件。
-
檢測並重載
[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
注意: 使用reload而不使用restart的好處是能避免因配置文件中存在錯誤而無法正常啓動!當配置文件有錯誤時reload是不會生效的,也就是說不會破壞原來的nginx服務。
-
添加指定目錄並測試
添加虛擬主機中指定的目錄:
[root@localhost vhost]# mkdir /data/wwwroot/test.com
[root@localhost vhost]# vim /data/wwwroot/test.com/index.html
test.com測試:
[root@localhost vhost]# curl -uhuang:159820 -x 127.0.0.1:80 test.com
test.com -
定義訪問目錄用戶認證:
編輯虛擬主機配置文件:
[root@localhost vhost]# vim test.com.conf
......
location /admin/ #指定用戶認證的目錄
......創建目錄:
[root@localhost vhost]# mkdir /data/wwwroot/test.com/admin/
[root@localhost vhost]# vim /data/wwwroot/test.com/admin/index.html
test.com admin dir檢測並重載配置文件:
[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 -x 127.0.0.1:80 test.com訪問test.com不需要用戶認證直接訪問
[root@localhost vhost]# curl -x 127.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.8.0</center>
</body>
</html>
[root@localhost vhost]# curl -uhuang:159820 -x 127.0.0.1:80 test.com/admin/
test.com admin dir訪問test.com/admin/需要用戶認證。
-
針對url用戶認證
修改虛擬主機中location:
location ~ admin.php訪問test.com和admin不需要用戶認證,訪問admin.php 需要用戶認證
Nginx域名重定向
編輯虛擬主機配置文件:
[root@localhost vhost]# 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;
# 主域名爲test.com,輸入其它域名會跳轉到主域名。
}
檢測並重載配置:
[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 test2.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.8.0
Date: Wed, 03 Jan 2018 15:37:44 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/index.html
#訪問test2.com、test3.com都會跳轉到test.com。訪問test.com會提示404.