Nginx的官网:http://nginx.org 可以看到nginx的更新速度是很快的,这说明目前使用nginx的用户越来越多。Apache现在是在走下坡路了。
nginx软件比较小巧,功能强大,又很很多的企业在研究Nginx的模块,虽然nginx支持的功能不是很多,但是可以扩展第3方的模块进来。
Apache 、php、nginx都是支持模块加入的。
Nginx应用场景:web服务、反向代理、负载均衡
wget http://211.162.209.171/files/40210000041973DC/nginx.org/download/nginx-1.12.2.tar.gz
2.下载完成后解压缩
tar zxvf nginx-1.12.2.tar.gz
3 . 进入到该目录下,执行以下命令
./configure --prefix=/usr/local/nginx
4 . 接着执行make && make install
完成后会在/usr/local/nginx目录下生成几个子目录:
conf :配置文件目录
html : 样例文件
lons : 存放日志目录
sbin : 进程或者说核心文件
5 . 创建启动脚本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
6 . 加入服务列表并设置为开机启动
chkconfig --add nginx
chkconfig nginx on
7 配置文件
cd /usr/local/nginx/conf //切换到该目录下,用ls可以到nginx.conf
mv nginx.conf nginx.conf.bak //
将它改名为nginx.conf.bak
但是我们并不使用它,改名是为了创建新的配置文nginx.conf ,写入以下内容
user nobody nobody; //定义由谁启动nginx服务
worker_processes 2; //定义启动后的子进程有2个
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200; //最多可以打开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 //对应虚拟主机,没有server也就没有对应的域名、网站,只有定义后才能正常访问
{
listen 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; //nginx通过该行来调用php-fpm服务。指定php-fpm监听sock
# fastcgi_pass 127.0.0.1:9000 //示例指定IP和端口
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}
8 .保存后使用/usr/local//nginx/sbin/nginx -t来检查配置文件语法及测试是否成功
9 . 启动nginx服务并查看进程
/etc/init.d/nginx start
ps aux |grep nginx
10 .测试
都会出现下面的内容
curl localhost 或者curl 127.0.0.1
11.测试是否解析php
首先vi /usr/local/nginx/html/1.php加入如下内容echo "This is nginx test page.";
再crul localhost ,如果是这样的输出,那么表示是能解析php的
include vhost/*.conf;
如下
server
{
listen 80 default_server; //有了default_server这个标记就是默认虚拟主机
server_name zcy.com;
index index.html index.htm index.php; //指定索引页
root /data/wwwroot/default; //
}
~
创建/data/wwwroot/default目录下,然后进去到这里 ,然后定义vi index.html
在其中写入 This is the default site.
检查是否有错误
/usr/local/nginx/sbin/nginx -t
重新加载
/usr/local/nginx/sbin/nginx -s reload
测试
[root@chunt vhost]# curl localhost
This is the default site. //这就是刚才定义的index.html
[root@chunt vhost]#
[root@chunt vhost]# curl -x127.0.0.1:80 123.com
This is the default site.
[root@chunt vhost]#
[root@chunt vhost]# curl -x127.0.0.1:80 ADC.com
This is the default site.
[root@chunt vhost]#
[root@chunt vhost]#
[root@chunt vhost]# curl -x127.0.0.1:80 zcy.com
This is the default site.
[root@chunt vhost]#
不管什么域名,只要解析过来指向该服务器,都能访问这个站点。
vi /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/htpsaawd; //用户名密码文件
}
}
/usr/local/apache/bin/htpasswd
, 如果没有安装过apache,那么可以使用yum安装http,然后在直接使用htpasswd来生成[root@chunt vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd yiyi
New password:
Re-type new password:
Adding password for user yiyi
如果需要创建多个用户,那么第2次创建的时候就不可以使用-c选项了,不然会将之前的那一个用户的密码文件和用户名删除掉,
[root@chunt vhost]# htpasswd /usr/local/nginx/conf/htpasswd lufei
New password:
Re-type new password:
Adding password for user lufei
[root@chunt vhost]# !cat
cat /usr/local/nginx/conf/htpasswd
yiyi:$apr1$t6/GQM/D$Gb4PsAmbCyEQbQpvqjB86.
lufei:$apr1$4FbM5Jl6$T084c3cEUh5D3iGsbh0Yr1
[root@chunt vhost]#
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload //重新加载的好处在于配置文件中有错误时,它是不会生效的,不会破坏原来的nginx服务。
[root@chunt vhost]# curl -x127.0.0.1:80 test.com
<html>
<head><title>401 Authorization Requiredtitle>head>
<body bgcolor="white">
<center><h1>401 Authorization Requiredh1>center>
<hr><center>nginx/1.12.2center>
body>
html>
[root@chunt vhost]#
[root@chunt vhost]# curl -x127.0.0.1:80 test.com -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.12.2
Date: Tue, 13 Mar 2018 17:37:58 GMT
Content-Type: text/html
Content-Length: 195
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"
[root@chunt vhost]#
这里显示了401状态码,那么需要指定用户才可以了
mkdir /data/wwwroot/test.com
echo "nihao chongqing" > /data/wwwroot/test.com/index.html
[root@chunt vhost]# curl -x127.0.0.1:80 test.com
<html>
<head><title>401 Authorization Requiredtitle>head>
<body bgcolor="white">
<center><h1>401 Authorization Requiredh1>center>
<hr><center>nginx/1.12.2center>
body>
html>
[root@chunt vhost]# curl -uyiyi:1234 -x127.0.0.1:80 test.com
nihao chongqing
如果正常的话这里是可以看到test.com输出的.
1.如何定义
location /admin //修该目录名即可。
2.检查并加载配置文件
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
3 .测试
[root@chunt vhost]# curl -x127.0.0.1:80 test.com
nihao chongqing
[root@chunt vhost]#
[root@chunt vhost]# curl -x127.0.0.1:80 test.com/admin/
<html>
<head><title>401 Authorization Requiredtitle>head>
<body bgcolor="white">
<center><h1>401 Authorization Requiredh1>center>
<hr><center>nginx/1.12.2center>
body>
html>
[root@chunt vhost]#
[root@chunt vhost]# mkdir /data/wwwroot/test.com/admin
mkdir: 无法创建目录"/data/wwwroot/test.com/admin": 文件已存在
[root@chunt vhost]#
[root@chunt vhost]# echo "yangqin nihao" > /data/wwwroot/test.com/admin/index.html
[root@chunt 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@chunt vhost]#
[root@chunt vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@chunt vhost]#
[root@chunt vhost]# curl -x127.0.0.1:80 test.com/admin
<html>
<head><title>301 Moved Permanentlytitle>head>
<body bgcolor="white">
<center><h1>301 Moved Permanentlyh1>center>
<hr><center>nginx/1.12.2center>
body>
html>
[root@chunt vhost]# curl -x127.0.0.1:80 test.com/admin/
<html>
<head><title>401 Authorization Requiredtitle>head>
<body bgcolor="white">
<center><h1>401 Authorization Requiredh1>center>
<hr><center>nginx/1.12.2center>
body>
html>
[root@chunt vhost]# curl -uyiyi:1234 -x127.0.0.1:80 test.com/admin/
yangqin nihao
[root@chunt vhost]#
location ~ admin.php
,检查并重新加载配置。这时候才去访问test.com/admin/时就不需要加-u选项了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; //permanent是301 redirect是302
}
location ~ admin.php
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
~
~
~
~
在server_name后面再加一个域名test2.com,在apache中的server_name中并不支持写多个域名,只认第一个。要想跟多少可以,要使用server_alias配置段来定义。在nginx这里是可以写多个的域名,比如test2.com、test3.com,需要访问这2个域名的时候跳转到test.com上。
[root@chunt vhost]# curl -x127.0.0.1:80 test2.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Wed, 14 Mar 2018 17:09:36 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html
[root@chunt vhost]# curl -x127.0.0.1:80 test2.com/admin/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Wed, 14 Mar 2018 17:09:55 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin/index.html
[root@chunt vhost]# curl -x127.0.0.1:80 test2.com/admin/index.html/1232424 -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Wed, 14 Mar 2018 17:10:21 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin/index.html/1232424
[root@chunt vhost]# curl -x127.0.0.1:80 test4.com/index.html -I //这里test4并没有在其中,curl它的时候就会去访问默认虚拟主机。
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 14 Mar 2018 17:10:53 GMT
Content-Type: text/html
Content-Length: 16
Last-Modified: Wed, 14 Mar 2018 15:13:06 GMT
Connection: keep-alive
ETag: "5aa93c02-10"
Accept-Ranges: bytes
[root@chunt vhost]#