Nginx安裝
進入存放源碼包的目錄:
cd /usr/local/src下載源碼包:
wget http://nginx.org/download/nginx-1.12.1.tar.gz解壓:
tar zxf nginx-1.12.1.tar.gz安裝Nginx:
./configure --prefix=/usr/local/nginx
編譯安裝:
make && make install編輯Nginx啓動腳本:
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
修改啓動腳本的權限:
chmod 755 /etc/init.d/nginx添加Nginx服務:
chkconfig –add nginx設置開機啓動:
chkconfig nginx on配置Nginx的配置文件,因爲Nginx下已經有nginx.conf這個配置文件,我們不用它,把它備份起來然後用自己的配置:
cd /usr/local/nginx/conf/
mv nginx.conf nginx.conf.bak編輯配置文件:
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; //定義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 //每一個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;
}
}
}
測試配置文件語法:
/usr/local/nginx/sbin/nginx -t開啓Nginx:
/etc/init.d/nginx startNginx對應的是80端口:
netstat -lntp |grep 80測試Nginx解析php:
vi /usr/local/nginx/html/1.php
加入如下內容:
<?php
echo "test php scripts.";
?>
- 用curl測試:
curl localhost/1.php
Nginx默認虛擬主機
- 編輯配置文件:
vim /usr/local/nginx/conf/nginx.conf
增加:
include vhost/*.conf
創建vhost目錄:
mkdir /usr/local/nginx/conf/vhost進入vhost目錄下並創建編輯一個.conf文件:
cd /usr/local/nginx/conf/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;
}
創建default目錄:
mkdir /data/wwwroot/default/寫一些內容在default目錄下的index.html文件中:
echo “This is a default site.”>/data/wwwroot/default/index.html測試語法:
/usr/local/nginx/sbin/nginx -t重新加載配置文件,不需要重啓服務:
/usr/local/nginx/sbin/nginx -s reloadcurl測試:
curl localhost
curl -x127.0.0.1:80 123.com
就算訪問的不是aaa.com,只要解析過來,指向到我們服務器,都能訪問到這個站點,這就是默認虛擬主機。
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;
}
}
創建test.com目錄:
mkdir /data/wwwroot/test.com在test.com目錄下編輯index.html:
echo “test.com”>/data/wwwroot/test.com/index.html如果之前沒有安裝過Apache的話就安裝httpd,爲了是可以使用Apache的htpasswd工具:
* yum install -y httpd*Apache自帶命令htpasswd創建密碼文件,-c是創建,-m是指定md5加密類型,指定用戶爲xie(PS:如果再次新增用戶,就不需要再加 -c ,因爲已經創建過密碼文件了):
/usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd xie
測試配置並重新加載:
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reloadcurl測試:
curl -x127.0.0.1:80 test.com
這裏出現401,說明需要用戶密碼認證
當我們用-u加上用戶名和密碼時就可以訪問了
針對目錄的用戶認證:
在location後面加上目錄名字即可
用curl測試,訪問網站時是正常的,只有訪問admin目錄下時就會出現401,需要用戶認證
當我們指定用戶密碼後就可以正常訪問了
針對文件的用戶認證:
在location後面加上匹配文件名字,下圖的意思就是匹配到admin.php這個文件就需要用戶認證
curl測試,訪問admin目錄時是正常的,訪問admin.php就需要用戶認證了
Nginx域名重定向
- 更改test.com.conf
server
{
listen 80;
server_name test.com test1.com test2.com;
//server_name後面支持寫多個域名,這裏要和httpd的做一個對比
index index.html index.htm index.php;
root /data/wwwroot/test.com;
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
//permanent爲永久重定向,狀態碼爲301,如果寫redirect則爲302
}
}
設置好後-t,-s測試加載配置文件
curl測試:
訪問test2.com後會跳轉到test.com
擴展
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