Nginx安裝
進入/usr/local/src目錄下,下載一個Nginx的穩定版。
[root@shuai-01 ~]# cd /usr/local/src
[root@shuai-01 src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
解壓壓縮包:
[root@shuai-01 src]# tar zxvf nginx-1.12.2.tar.gz
編譯:
進入Nginx目錄下
編譯根據自己的要求來編譯,選擇一些模塊。
我這裏只指定文件路徑:
[root@shuai-01 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx
接着make:
[root@shuai-01 nginx-1.12.2]# make
make install:
[root@shuai-01 nginx-1.12.2]# make install
[root@shuai-01 nginx-1.12.2]# ls /usr/local/nginx/
conf html logs sbin
make時候出錯:
make
make: * No rule to make target build', needed by
default’. Stop.
安裝gcc
yum install gcc
解決辦法是需要安裝openssl以及ncurses組件
yum install -y openssl*
yum -y install ncurses-devel
conf : 配置文件目錄
html : 網頁樣例
logs: 日誌
sbin: 進程,核心文件
給配置文件做一個啓動腳本(/etc/init.d/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
- 1
- 2
更改配置文件權限:
[root@shuai-01 nginx-1.12.2]# chmod 755 /etc/init.d/nginx
添加爲系統服務,並設置爲開機自啓動:
[root@shuai-01 nginx-1.12.2]# chkconfig --add nginx
[root@shuai-01 nginx-1.12.2]# chkconfig nginx on
配置Nginx的配置文件:
Nginx的conf目錄下有nginx.conf這個文件了。
[root@shuai-01 nginx-1.12.2]# cd /usr/local/nginx/conf
[root@shuai-01 conf]#
[root@shuai-01 conf]# ls
fastcgi.conf koi-win scgi_params
fastcgi.conf.default mime.types scgi_params.default
fastcgi_params mime.types.default uwsgi_params
fastcgi_params.default nginx.conf uwsgi_params.default
koi-utf nginx.conf.default win-utf
不用,用自己創建的配置文件。
先將nginx.conf改名
[root@shuai-01 conf]# mv nginx.conf nginx.conf.1
自己創建
[root@shuai-01 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;
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;
}
}
}
編輯配置文件後,檢查語法:
[root@shuai-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
啓動nginx
[root@shuai-01 conf]# /etc/init.d/nginx start
Starting nginx (via systemctl): [ 確定 ]
user 用來定義啓動nginx服務的是那個用戶。
worker_processes 2; //子進程有兩個
error_log /usr/local/nginx/logs/nginx_error.log crit; //錯誤日誌
worker_rlimit_nofile 51200;//nginx最多打開多少個文件
server 和Apache配置文件中的virtual hosts 一樣,對應虛擬主機。
server
{
listen 80; //監聽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; //指定PHP-fpm的監聽端口或者監聽sock
監聽127.0.0.1:9000
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
} // 配置解析PHP的部分
}
nginx配置詳解:
http://www.ha97.com/5194.html
https://my.oschina.net/duxuefeng/blog/34880
nginx默認虛擬主機
設置Nginx默認虛擬主機,其實默認就設置了。在Nginx的配置文件中,server就是。一般的,你有幾個網站就設置幾個server。還有另一種設置方式,在配置文件中不要去設置server,直接重新寫一個虛擬主機配置文件(vhost/*.conf).
編輯配置文件,把Nginx配置文件中server段刪去,添加一段:
include vhost/*.conf;
在/usr/local/nginx/conf/目錄下,創建一個目錄(vhost),並在目錄下創建一個新文件。這個vhost就類似於Apache中的虛擬配置文件。
[root@shuai-01 conf]# mkdir vhost
[root@shuai-01 conf]# cd vhost/
[root@shuai-01 vhost]# ls
[root@shuai-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;
}
創建/data/wwwroot/default,並在defualt目錄下編寫index.html文件:
[root@shuai-01 vhost]# mkdir /data/wwwroot/default
[root@shuai-01 vhost]# cd /data/wwwroot/default/
[root@shuai-01 default]# vim index.html
echo “This is a default site.”>/data/wwwroot/default/index.html
檢測一下配置文件語法
[root@shuai-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@shuai-01 default]# /usr/local/nginx/sbin/nginx -s reload
一般的,在服務器跑動的時候,都選擇重新加載配置文件,而不是重啓服務(/etc/init.d/nginx restart),重啓服務會短暫關閉然後在啓動。
測試默認主機:
[root@shuai-01 default]# curl localhost
this is a default site
默認虛擬主機就是隻要你解析過來是這個IP,不管什麼域名,都會訪問到默認虛擬主機。
[root@shuai-01 default]# curl -x127.0.0.1:80 bbb.com
this is a default site
[root@shuai-01 default]# curl -x127.0.0.1:80 abadsj.com
this is a default site
Nginx用戶認證
做用戶認證就是爲了安全,在做httpd的用戶認證時就已經說到過,可以將兩篇帖子結合起來看。
http://blog.csdn.net/aoli_shuai/article/details/78854280
創建一個虛擬主機(test.com.conf):
[root@shuai-01 wwwroot]# cd /usr/local/nginx/conf/vhost/
[root@shuai-01 vhost]# ls
aaa.com.conf
[root@shuai-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 /
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;//用戶名密碼文件
}
}
生成密碼的工具是htpasswd,這個工具在Apache用戶認證時就安裝過了,沒安裝的就用yum install -y httpd 安裝上。
爲shuai用戶做用戶認證:
[root@shuai-01 vhost]# /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd shuai
New password:
Re-type new password:
Adding password for user shuai
爲aoli用戶做認證:
[root@shuai-01 vhost]# /usr/local/apache2.4/bin/htpasswd /usr/local/nginx/conf/htpasswd aoli
New password:
Re-type new password:
Adding password for user aoli
[root@shuai-01 vhost]# cat /usr/local/nginx/conf/htpasswd
shuai:$apr1$V0AiFAbc$CSttuCLed5mA1AMi1mKdw/
aoli:$apr1$OX2YLAuw$z1XF5XGLO/Z15Qw5dFo0V0
檢查配置文件語法並重新加載配置文件:
[root@shuai-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@shuai-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
測試:
[root@shuai-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.2</center>
</body>
</html>
出現401,需要用戶認證。
[root@shuai-01 vhost]# curl -ushuai: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.2</center>
</body>
</html>
出現404,沒有文件。
寫一個index.html文件。
[root@shuai-01 vhost]# mkdir /data/wwwroot/test.com
[root@shuai-01 vhost]# echo "test.com" > /data/wwwroot/test.com/index.html
[root@shuai-01 vhost]# curl -ushuai:123456 -x127.0.0.1:80 test.com
test.com
這個用戶認證時針對整個站點,只針對某個特定目錄的用戶認證。針對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;
}
}
檢查配置文件語法並重新加載配置文件:
[root@shuai-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@shuai-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
測試:
訪問test.com
[root@shuai-01 vhost]# curl -x127.0.0.1:80 test.com
test.com
訪問test.com/admin
[root@shuai-01 vhost]# curl -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.2</center>
</body>
</html>
針對某個.php文件
配置文件寫成
location ~ admin.php
nginx域名重定向
更改虛擬配置文件
[root@shuai-01 vhost]# vim test.com.conf
server
{
listen 80;
server_name test.com test1.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 /admin/
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
這裏多個域名都可以寫到server_name 後面,不像httpd,有server_alias .
檢查配置文件語法並重新加載配置文件:
[root@shuai-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@shuai-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
測試:
[root@shuai-01 vhost]# curl -x127.0.0.1:80 test2.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Mon, 08 Jan 2018 11:16:30 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html
[root@shuai-01 vhost]# curl -x127.0.0.1:80 test3.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Mon, 08 Jan 2018 11:16:42 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html
[root@shuai-01 vhost]# curl -x127.0.0.1:80 test4.com/index.html -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Mon, 08 Jan 2018 11:16:49 GMT
Content-Type: text/html
Content-Length: 23
Last-Modified: Mon, 08 Jan 2018 07:52:14 GMT
Connection: keep-alive
ETag: "5a53232e-17"
Accept-Ranges: bytes
測試test2.com test3.com 都是301重定向,test4.com 時,訪問就是默認虛擬主機。
nginx rewrite四種flag http://www.netingcn.com/nginx-rewrite-flag.html