1、安裝nginxjavascript
[root@lnmp conf]# wget http://nginx.org/download/nginx-1.8.0.tar.gzphp
[root@lnmp conf]# tar zxvf nginx-1.8.0.tar.gzcss
[root@lnmp conf]# cd nginx-1.8.0html
[root@lnmp conf]# ./configure --prefix=/usr/local/nginxjava
[root@lnmp conf]# make && make installnode
[root@lnmp conf]# vim /etc/init.d/nginxnginx
#!/bin/bashvim
# chkconfig: - 30 21bash
# description: http service.app
# 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@lnmp conf]# chmod 755 /etc/init.d/nginx
[root@lnmp conf]# chkconfig --add nginx
[root@lnmp conf]# chkconfig nginx on
[root@lnmp conf]# mv nginx.conf nginx.conf.bak
[root@lnmp conf]# vim /usr/local/nginx/conf/nginx.conf
user nobody nobody; (啓動nginx的用戶)
worker_processes 2; (定義子進程)
error_log /usr/local/nginx/logs/nginx_error.log crit; (錯誤日誌)
pid /usr/local/nginx/logs/nginx.pid; (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 (http服務)
{
listen 80; (監聽80端口)
server_name localhost; (設置域名)
index index.html index.htm index.php; (設置主頁)
root /usr/local/nginx/html; (設置訪問主目錄)
location ~ \.php$ (定義php解析)
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
#fastcgi_pass 127.0.0.1:9000; (和上面一行的意思相同,只是不一樣的寫法,監聽127.0.0.1:9000)
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}
[root@lnmp conf]# /usr/local/nginx/sbin/nginx -t (檢查語法錯誤)
[root@lnmp conf]# /etc/init.d/nginx start (啓動nginx)
[root@lnmp conf]# netstat -lntp |grep 80 (查看80端口)
[root@lnmp conf]# ps aux |grep nginx (查看nginx服務,可看到2個work子進程)
2、nginx默認虛擬主機
Nginx默認主機:
[root@lnmp ~]# vim /usr/local/nginx/conf/nginx.conf (刪除server及下面的,在http最後添加)
include vhost/*.conf; (指定虛擬主機目錄,並讀取以.conf結尾的文件)
:wq退出保存
[root@lnmp ~]# mkdir /usr/local/nginx/conf/vhost (建立虛擬主機目錄)
[root@lnmp ~]# vim aaa.com.conf (建立虛擬主機配置文件並添加如下內容:)
server
{
listen 80 default_server; (紅色的字表示設置這個虛擬主機爲默認虛擬主機)
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
}
[root@lnmp vhost]# mkdir -p /data/wwwroot/default/ (建立虛擬主機的訪問目錄)
[root@lnmp vhost]# echo "This is a default site." >/data/wwwroot/default/index.html (編寫虛擬主機主頁)
[root@lnmp vhost]# /usr/local/nginx/sbin/nginx -t (檢查配置文件語法錯誤)
[root@lnmp vhost]# /usr/local/nginx/sbin/nginx -s reload (從新加載配置文件)
[root@lnmp vhost]# curl localhost (curl本機,發現到達nginx的虛擬主機主頁)
This is a default site.
!!:還有一個須要注意的是,若是不加紅色字體的字段,再找server時會根據文件名排序,好比:aaa.com.cnf和bbb.com.cnf,aaa確定是在前,因此aaa.com.cnf是默認虛擬主機
3、Nginx用戶認證
nginx用戶認證
用到了以前httpd的htpasswd功能。
[root@lnmp ~]# 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 (定義用戶名密碼文件)
}
}
由於要使用到httpd的htpasswd功能,則須要安裝httpd,能夠直接yum安裝,直接敲htpasswd命令,
[root@lnmp ~]# htpasswd -c /usr/local/nginx/conf/htpasswd lty (c是生成用戶文件,若要添加則不須要,不然會覆蓋原文件)
New password:
Re-type new password:
Adding password for user lty
檢查語法錯誤,而且從新加載配置文件:(若是配置文件出現錯誤,reload不會使錯誤的配置文件生效)
[root@lnmp ~]# /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@lnmp ~]# /usr/local/nginx/sbin/nginx -s reload
檢測:
[root@lnmp ~]# curl -x127.0.0.1:80 test.com -I (不加用戶發現401,須要用戶認證)
HTTP/1.1 401 Unauthorized
Server: nginx/1.8.0
Date: Thu, 14 Dec 2017 04:15:02 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"
[root@lnmp ~]# curl -ulty:westos -x127.0.0.1:80 test.com (-u指定用戶和密碼後,返回值)
test.com
1.需求;訪問一個目錄或者文件時,才須要用戶認證。
實現:
[root@lnmp ~]# 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 /admin (用戶認證時加上admin目錄)
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
檢測語法錯誤而且從新加載配置文件:
[root@lnmp ~]# /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@lnmp ~]# /usr/local/nginx/sbin/nginx -s reload
檢測:
[root@lnmp ~]# curl -x127.0.0.1:80 test.com (訪問test.com時不須要密碼也能夠返回值)
test.com
[root@lnmp ~]# curl -x127.0.0.1:80 test.com/admin (訪問test.com下的admin時,401須要用戶認證)
<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>
需求,訪問test.com下的1.php須要用戶認證,
[root@lnmp ~]# 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 /admin/1.php (這裏修改匹配到1.php)
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
檢測語法錯誤而且從新加載配置文件:
[root@lnmp ~]# /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@lnmp ~]# /usr/local/nginx/sbin/nginx -s reload
檢測:
[root@lnmp ~]# curl -x127.0.0.1:80 test.com/admin/1.php (不加用戶密碼訪問發現401)
<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@lnmp ~]# curl -ulty:westos -x127.0.0.1:80 test.com/admin/1.php (加用戶密碼訪問則正常返回)
touch file.php
4、nginx域名重定向
httpd配置文件裏server_name後面不支持寫多個域名,就算寫了多個,也默認識別第一個
nginx的配置文件server_name後面則支持寫多個域名,
[root@lnmp ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
server
{
listen 80;
server_name test.com test1.com test2.com; (server_name後跟多個域名)
index index.html index.htm index.php;
root /data/wwwroot/test.com;
if ($host != 'test.com' ) { (若是域名不是test.com)
rewrite ^/(.*)$ http://test.com/$1 permanent; (rewrite到test.com,permanent301報錯 redirect302報錯)
檢查語法錯誤而且從新加載配置文件:
[root@lnmp ~]# /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@lnmp ~]# /usr/local/nginx/sbin/nginx -s reload
檢測:
[root@lnmp ~]# curl -x127.0.0.1:80 test1.com/admin/1.php -I (訪問test1時,提示301,跳轉到test.comx下)
HTTP/1.1 301 Moved Permanently
Server: nginx/1.8.0
Date: Thu, 14 Dec 2017 05:03:32 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/admin/1.php
[root@lnmp ~]# curl -x127.0.0.1:80 test2.com/admin/1.php -I (訪問test2時,提示301,跳轉到test.comx下)
HTTP/1.1 301 Moved Permanently
Server: nginx/1.8.0
Date: Thu, 14 Dec 2017 05:03:38 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/admin/1.php
5、Nginx配置文件詳解