1、nginx安装:
(1):下载nginx安装包;
[root@localhost_001 src]# wget http://nginx.org/download/nginx-1.4.7.tar.gz
(2):解压;
[root@localhost_001 src]# tar -zxvf nginx-1.4.7.tar.gz
(3):初始化nginx; ./configure --prefix=/usr/local/nginx
[root@localhost_001 nginx-1.4.7]# ./configure --prefix=/usr/local/nginx
(4):make && make install;
[root@localhost_001 nginx-1.4.7]# make && make install
(5):查看nginx的目录;
[root@localhost_001 nginx-1.4.7]# ls /usr/local/nginx/
conf html logs sbin
conf:配置文件目录;
html:样例文件;
logs:存放日记;
sbin:核心进程目录;(支持 -t 语法检测错误)
(6):创建nginx的启动脚本;也可以这里复制:nginx启动脚本配置
[root@localhost_001 nginx-1.4.7]# 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_001 nginx-1.4.7]#
(7):更改启动权限;
[root@localhost_001 nginx-1.4.7]# vim /etc/init.d/nginx
(8):nginx加入到服务列表,并开机启动nginx;
[root@localhost_001 nginx-1.4.7]# chkconfig --add nginx
[root@localhost_001 nginx-1.4.7]# chkconfig nginx on
[root@localhost_001 nginx-1.4.7]# chkconfig --list nginx
nginx 0:关 1:关 2:开 3:开 4:开 5:开 6:关
(9):创建nginx的配置文件; /usr/local/nginx/conf/nginx.conf(默认存在)
注释:这里不用它自带的配置文件,我们自定义即可; nginx配置文件:nginx配置文件
[root@localhost_001 conf]# vim 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;
worker_rlimit_nofile 51200; #nginx最多可以打开多少个文件;
events
{
use epoll; #使用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这几行,那么nginx将监听不到80端口,导致服务不可用;
server
{
listen 80;
server_name localhost; 域名;
index index.html index.htm index.php;
root /usr/local/nginx/html; #根目录
location ~ \.php$
{
include fastcgi_params; #调用php;
fastcgi_pass unix:/tmp/php-fcgi.sock; 指定监听方式; 127.0.0.1:9000
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}
(10):检测是否存在错误; -t
[root@localhost_001 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
(11):启动nginx;
[root@localhost_001 conf]# service nginx start
Starting nginx (via systemctl): [ 确定 ]
详细;安装步骤如下;
[root@localhost_001 src]# wget http://nginx.org/download/nginx-1.4.7.tar.gz
--2018-10-16 10:17:18-- http://nginx.org/download/nginx-1.4.7.tar.gz
100%[===========================================================================================>]
2018-10-16 10:17:24 (148 KB/s) - 已保存 “nginx-1.4.7.tar.gz” [769153/769153])
[root@localhost_001 src]# tar -zxvf nginx-1.4.7.tar.gz #解压
#生成编译所需要的文件;
[root@localhost_001 src]# cd nginx-1.4.7
[root@localhost_001 nginx-1.4.7]# ./configure --prefix=/usr/local/nginx
[root@localhost_001 nginx-1.4.7]# make && make install
#创建nginx启动脚本:
[root@localhost_001 nginx-1.4.7]# vim /etc/init.d/nginx
[root@localhost_001 nginx-1.4.7]# vim /etc/init.d/nginx #更改启动权限;
[root@localhost_001 nginx-1.4.7]# chkconfig --add nginx
[root@localhost_001 nginx-1.4.7]# chkconfig nginx on
[root@localhost_001 nginx-1.4.7]# chkconfig --list nginx
nginx 0:关 1:关 2:开 3:开 4:开 5:开 6:关
更改nginx的配置文件内容:
[root@localhost_001 conf]# vim nginx.conf
[root@localhost_001 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
[root@localhost_001 conf]# service nginx start
Starting nginx (via systemctl): [ 确定 ]
2:查看nginx相关进程;
[root@localhost_001 conf]# ps aux |grep nginx
root 3429 0.0 0.0 24844 784 ? Ss 10:59 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 3430 0.0 0.1 27148 3356 ? S 10:59 0:00 nginx: worker process
nobody 3431 0.0 0.1 27148 3356 ? S 10:59 0:00 nginx: worker process
root 3434 0.0 0.0 112720 968 pts/0 R+ 11:00 0:00 grep --color=auto nginx
3、测试nginx和php; 浏览器里输入:curl localhost
[root@localhost_001 ~]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@lo
用浏览器访问:IP地址;
测试php;
hello nginx.[root@localhost_001 ~]# cat /usr/local/nginx/html/1.php
<?php
echo "hello nginx.";
[root@localhost_001 ~]# curl localhost/1.php
hello nginx.[root@localhost_001 ~]#
2、默认虚拟主机;
(1):首先需要删除配置/usr/local/nginx/conf/nginx.conf里:server的那一段;然后添加 include vhost/.conf
[root@localhost_001 conf]# cat 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;
include vhost/*.conf; #新增这一行,必须的;(删除服务的那些内容)
}
(2):创建虚拟主机目录vhost,在其目录下配置虚拟主机; /usr/local/nginx/conf/vhost/aaa.com.conf
[root@localhost_001 conf]# mkdir /usr/local/nginx/conf/vhost
[root@localhost_001 conf]# cd vhost/
[root@localhost_001 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;
}
(3):创建网站根目录及默认页;
[root@localhost_001 vhost]# mkdir -p /data/wwwroot/default
[root@localhost_001 vhost]# cd /data/wwwroot/default/
[root@localhost_001 default]# vim index.html
This is a default site.
(4):检测并重启nginx服务;
[root@localhost_001 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
[root@localhost_001 conf]# service nginx restart
Restarting nginx (via systemctl): [ 确定 ]
注释:或者重新加载也可以; /usr/local/nginx/sbin/nginx -s reload
测试默认页;
[root@localhost_001 conf]# curl localhost
This is a default site.
[root@localhost_001 conf]# curl localhost:80 bbb.com
This is a default site.
注释:nginx支持include这种语法;
1.默认虚拟主机,是根据目录的第一个.conf了进行选择,所以只需要在vhost目录下依次创建就可以了,然后依次查找即可;当然这种方法不智能 2.只需要在vhost目录的.conf配置文件内,加上一个“default_server ”即可,把当前的这个配置对应的网站设置为第一个默认虚拟主机;
3、用户验证;
针对真个网站做验证;
(1):再此新建一个虚拟主机test.com.conf //内容如下:
[root@localhost_001 vhost]# vim test.com.conf
[root@localhost_001 vhost]# cat test.com.conf
server
{
listen 80;
server_name test.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
location / #表示全站,都需要进行用户认证
#location /admin #这个地方只要加上” /admin ” 就变成 针对这个站点的“admin” 这个目录需要用户认证
#location ~ admin.php #如果把这行这样写,就会变成,匹配 “ admin.php ”这个页面的时候才需要用户认证
{
auth_basic "Auth"; #定义用户认证的名字
auth_basic_user_file /usr/local/nginx/conf/htpasswd; #用户名密码文件
}
}
创建网站相关配置文件;
[root@localhost_001 vhost]# mkdir /data/wwwroot/test.com
[root@localhost_001 vhost]# echo "test.com fast" > /data/wwwroot/test.com/index.html
(2):验证的话需要生成密码文件,这时也用到了apache的密码生成工具;
注释:若本机已经安装过httpd,则可以直接使用下面httpasswd生成; -c后面跟生成的目录位置及用户名;
htpasswd -c /usr/local/nginx/conf/htpasswd fenye
安装并使用htpasswd生成密码文件;
[root@localhost_001 vhost]# /usr/bin/htpasswd -c /usr/local/nginx/conf/htpasswd fenye
New password:
Re-type new password:
Adding password for user fenye
[root@localhost_001 vhost]# cat /usr/local/nginx/conf/htpasswd
fenye:$apr1$hg.HAMRG$A9SkSCe6vomTaxlmT9s7t0
注释:关于htpasswd这个工具在第一次使用需要使用-c来创建这个文件,第二次使用的时候因为已经有htpasswd这个文件,则不需要使用-c来创建,如若强制使用,将覆盖之前的文件;
[root@localhost_001 vhost]# /usr/bin/htpasswd /usr/local/nginx/conf/htpasswd user1
New password:
Re-type new password:
Adding password for user user1
[root@localhost_001 vhost]# cat /usr/local/nginx/conf/htpasswd
fenye:$apr1$hg.HAMRG$A9SkSCe6vomTaxlmT9s7t0
user1:$apr1$yfKA2qd/$X9IatIA9XfweuEVvyUngU/
(3):检测配置文件是否有错误,并重新加载配置文件;
[root@localhost_001 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_001 vhost]# /usr/local/nginx/sbin/nginx -s reload
注释:使用 -s reload 重新加载配置和 restart重启服务的区别;
使用 -s reload 若配置文件存在错误,则配置文件生效;
使用 restart 重启,若配置文件存在错误,则会影响到网站的运行;
(4):测试,用curl命令访问: curl -x127.0.0.1:80 test.com
[root@localhost_001 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.4.7</center>
</body>
</html>
注释:这时候提示状态码401,则需要验证; 下面用curl命令来验证;
此时使用创建的两个用户fenye和user1来测试访问;
[root@localhost_001 vhost]# curl -ufenye:nihao123! -x127.0.0.1:80 test.com
test.com fast
[root@localhost_001 vhost]# curl -uuser1:nihao123@ -x127.0.0.1:80 test.com
test.com fast
注释:如上的验证时针对真个网站来的;
那如何针对一个目录做验证了;
比如当访问admin目录时,才需要做验证;
首先创建admin目录,并新建访问默认页,如下;
[root@localhost_001 vhost]# mkdir /data/wwwroot/test.com/admin
[root@localhost_001 vhost]# echo "test.com admin dir" > /data/wwwroot/test.com/admin/index.html
(1):尝试访问下,可以正常访问;
[root@localhost_001 vhost]# curl -uuser1:nihao123 -x127.0.0.1:80 www.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.4.7</center>
</body>
</html>
(2):修改虚拟主机配置文件; /usr/local/nginx/conf/vhost/test.com.conf
[root@localhost_001 vhost]# cat test.com.conf
server
{
listen 80;
server_name www.test.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
location /admin ####这个地方只要加上” /admin ” 就变成 针对这个站点的“admin” 这个目录需要用户认证
auth_basic "Auth"; #定义用户认证的名字
auth_basic_user_file /usr/local/nginx/conf/htpasswd; #用户名密码文件
}
}
注释:图示中location /admin这一行则表示对目录进行验证;
(3):然后检测配置文件,并重新加载配置文件;
[root@localhost_001 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_001 vhost]# /usr/local/nginx/sbin/nginx -s reload
(4):测试访问;发现访问www.test.com不需要验证,而访问它下面的/admin/目录则需要验证;显示Z;
[root@localhost_001 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost_001 vhost]# curl -x127.0.0.1:80 www.test.com
test.com fast
[root@localhost_001 vhost]# curl -x127.0.0.1:80 www.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.4.7</center>
</body>
</html>
(5):输入用户名和密码测试访问;
[root@localhost_001 vhost]# curl -ufenye:nihao123! -x127.0.0.1:80 www.test.com/admin/
test.com admin dir
[root@localhost_001 vhost]# curl -uuser1:nihao123@ -x127.0.0.1:80 www.test.com/admin/
test.com admin dir
3、针对URL做验证;
比如针对admin.php做验证;
1:修改虚拟主机配置文件/usr/local/nginx/conf/vhost/test.com.conf定义,在location后面加上 ~admin.php即可;
[root@localhost_001 vhost]# cat test.com.conf
server
{
listen 80;
server_name www.test.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
location ~ admin.php #如果把这行这样写,就会变成,匹配 “ admin.php ”这个页面的时候才需要用户认证
{
auth_basic "Auth"; #定义用户认证的名字
auth_basic_user_file /usr/local/nginx/conf/htpasswd; #用户名密码文件
}
}
2:创建admin.php的配置文件;
[root@localhost_001 vhost]# echo "test admin.php" > /data/wwwroot/test.com/admin.php
(3):检测配置文件是否错误然后重新加载配置文件;
[root@localhost_001 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_001 vhost]# /usr/local/nginx/sbin/nginx -s reload
(4):测试,使用curl命令来测试;
[root@localhost_001 vhost]# curl -x 127.0.0.1:80 www.test.com/admin.php
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.4.7</center>
</body>
</html>
输入密码后验证;
[root@localhost_001 vhost]# curl -uuser1:nihao123@ -x127.0.0.1:80 www.test.com/admin.php
test admin.php
[root@localhost_001 vhost]# curl -ufenye:nihao123! -x127.0.0.1:80 www.test.com/admin.php
test admin.php
4、nginx域名重定向;
在apache里面也有重定向,只是apache的 server_name只能跟一个域名,需要跟多个域名,则需要加alias;
在nginx里面 server_name需要设置多个域名,就会使网站的权重变了,那么到底使用哪个做为主域名了,这时候需要重定向功能了;
(1):修改虚拟主机配置文件; /usr/local/nginx/conf/vhost/test.com.conf
[root@localhost_001 vhost]# cat test.com.conf
server
{
listen 80;
server_name www.test.com bbs.test.com test1.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
if ($host != 'www.test.com' ) {
rewrite ^/(.*)$ http://www.test.com/$1 permanent;
}
}
注释: if ($host !='www.test.com') 表示加入域名不等于www.test.com,将执行下面的脚本;
注释:permanent是301的意思;
注释:redirect则表示302的意思;
(2):检测配置文件是否有错误,并重新加载配置文件;
[root@localhost_001 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_001 vhost]# /usr/local/nginx/sbin/nginx -s reload
(3):测试,用curl命令来测试;
[root@localhost_001 vhost]# curl -x127.0.0.1:80 bbs.test.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.4.7
Date: Tue, 16 Oct 2018 07:54:50 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://www.test.com/index.html
[root@localhost_001 vhost]# curl -x127.0.0.1:80 test1.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.4.7
Date: Tue, 16 Oct 2018 07:55:56 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://www.test.com/index.html
扩展
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