网站服务 代理服务 搭建Nginx服务器
nginx-1.8.0.tar.gz
rpm -q gcc gcc-c++
yum -y groupinstall "开发工具"
yum -y install pcre-devel openssl-devel
rpm -q httpd && service httpd stop
chkconfig httpd off
useradd -s /sbin/nologin nginx
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
make
make install
ls /usr/local/nginx/
conf 配置文件
nginx.conf 主配置文件
nginx.conf.default 模版文件
html 网页目录
logs 日志文件
sbin 命令
nginx
启动nginx 进程
/usr/local/nginx -h
常用选项
-v:查看nginx版本
-V:查看编译参数
-t:测试默认配置文件
-c:指定配置文件
[root@A nginx]# ./sbin/nginx
[root@A nginx]# netstat -utnlap | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7223/nginx
[root@A nginx]#
[root@A nginx]# ls logs/
access.log error.log nginx.pid
[root@A nginx]#
echo 123 > /usr/local/nginx/html/a.html
yum -y install elinks
elinks --dump http://localhost/a.html
123
Nginx进程管理
杀死nginx进程的方式
/usr/local/nginx/sbin/nginx -s stop
pkill -9 nginx
killall -9 nginx
kill -信号 nginx
TERM, INT 快速关闭 QUIT 从容关闭,关闭主进程及子进程HUP 重载配置文件
USR1 重新打开日志文件 USR2 平滑升级可执行程序
[root@A nginx]# /usr/local/nginx/sbin/nginx -s stop
[root@A nginx]# elinks --dump http://localhost/a.html
ELinks: 拒绝连接
[root@A nginx]#
[root@A nginx]# /usr/local/nginx/sbin/nginx
[root@A nginx]# elinks --dump http://localhost/a.html
123
[root@A nginx]#
kill -HUP `/usr/local/nginx/logs/nginx.pid`
elinks --dump http://localhost:8080/a.html
平滑升级nginx
22 tar -zxvf nginx-1.9.2.tar.gz
cd nginx-1.9.2
/usr/local/nginx/sbin/nginx -V
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
make
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginxold
mv objs/nginx /usr/local/nginx/sbin/
/usr/local/nginx/sbin/nginx -v
[root@A nginx-1.9.2]# make upgrade
/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
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
[root@A nginx-1.9.2]#
主配置文件的语法格式
http{
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
}
----------------------------------------------------------------
配置Nginx虚拟主机 server { }
基于端口的虚拟主机(通过端口区分客户端的访问)
端口 网页目录
http://192.168.1.254:80 /usr/local/nginx/html
http://192.168.1.254:8080 /wwwdir
vim nginx.conf
http {
server {
listen 80;
location / {
root html;
index a.html;
}
}
server {
listen 8080;
location / {
root /wwwdir;
index index.html;
}
}
}
-------------------------------------------------------------
基于域名的虚拟主机(通过主机名区分客户端的访问)
http://www.tarena.com /usr/local/nginx/html
http://bbs.tarena.com /bbsdir
192.168.1.254
vim nginxsername.conf
http{
server {
listen 80;
server_name www.tarena.com;
location / {
root html;
index index.html;
}
}
server {
listen 80;
server_name bbs.tarena.com;
location / {
root /bbsdir;
index index.html;
}
}
}
:wq
mkdir /bbsdir
echo 123 > /bbsdir/index.html
echo 567 > /usr/local/nginx/html/index.html
客户端 要能够解析主机名到web服务器的Ip地址
vim /etc/hosts
192.168.1.254 www.tarena.com www
192.168.1.254 bbs.tarena.com bbs
:wq
[root@A conf]# elinks --dump http://bbs.tarena.com
123
[root@A conf]# elinks --dump http://www.tarena.com
567
[root@A conf]#
-----------------------------------------------------------------
基于Ip地址的虚拟主机(通过ip地址区分客户端的访问)
http://1.0.0.254 /usr/local/nginx/html/
http://192.168.1.254 /bbsdir/
cat ip.conf
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 192.168.1.254:80;
location / {
root /bbsdir;
index index.html;
}
}
server {
listen 1.0.0.254:80;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
:wq
客户访问
ping 192.168.1.254
ping 1.0.0.254
[root@A ~]# elinks --dump http://1.0.0.254
567
[root@A ~]# elinks --dump http://192.168.1.254
123
[root@A ~]#
-----------------------------------------------------------------
客户端访问控制 location / { }
默认允许所有客户端访问网站目录下网页文件
location / {
.....
.....
allow 192.168.1.188;
deny all;
}
用户验证 (访问网页文件时,需要提交用户名和 密码)
location / {
.....
.....
allow 192.168.1.188;
deny all;
auth_basic "auth-domain";
auth_basic_user_file "/usr/local/nginx/conf/user.txt";}
rpm -q httpd-tools
[root@A conf]# htpasswd -c /usr/local/nginx/conf/user.txt webadmin
New password:
Re-type new password:
Adding password for user webadmin
[root@A conf]# cat /usr/local/nginx/conf/user.txt
webadmin:.GHgOK5P5MaiY
[root@A conf]#
-----------------------------------------------------------------
配置虚拟主机
http://192.168.1.254 /usr/local/nginx/html/
http://192.168.1.254:8090 /bbsdir/
但只允许从地址192.168.1.188客户端访问 网站服务器192.168.1.254的8090端口 并要提交正确的用户名和密码才可以访问
---------------------------------------------------------------
配置Nginx反向代理
vim nginx.conf
upstream "webgrp" {
server 192.168.1.1:80 weight=3;
server 192.168.1.2:80;
}
server {
listen 80;
location / {
#proxy_pass http://192.168.1.1;
proxy_pass http://webgrp;
}
}
:wq
nginx目前支持4种分配方式轮询(默认的): 逐一循环调度Weight:指定轮询几率,权重值和访问比率成正比
ip_hash:根据客户端IP分配固定的后端服务器Fair:按后端服务器响应时间短的优先分配
upstream sergrp {
#ip_hash;
#server 192.168.8.5:80 weight=2;
server 192.168.8.5:80 down;
server 192.168.8.4:8080;
server 192.168.8.6:80 backup;
server 192.168.8.3:80 max_fails=2 fail_timeout=30;
}