nginx-10939891
网站服务代理服务 搭建Nginx服务器nginx-1.8.0.tar.gz
rpm-qgcc gcc-c++
yum-y groupinstall"开发工具"
yum-yinstallpcre-devel openssl-devel
rpm-q httpd&&servicehttpdstop
chkconfig httpdoff
useradd-s/sbin/nologin nginx
./configure --prefix=/usr/local/nginx--user=nginx--group=nginx --with-http_ssl_module
make
makeinstall
ls/usr/local/nginx/
conf 配置文件
nginx.conf主配置文件
nginx.conf.default模版文件
html 网页目录
logs 日志文件
sbin 命令
nginx
启动nginx 进程
/usr/local/nginx -h
常用选项
-v:查看nginx版本
-V:查看编译参数
-t:测试默认配置文件
-c:指定配置文件
# ./sbin/nginx
# netstat -utnlap | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7223/nginx
#
# ls logs/
access.logerror.lognginx.pid
#
echo123 >/usr/local/nginx/html/a.html
yum-yinstall elinks
elinks--dumphttp://localhost/a.html
123
Nginx进程管理
杀死nginx进程的方式
/usr/local/nginx/sbin/nginx -s stop
pkill -9 nginx
killall-9nginx
kill -信号nginx
TERM, INT 快速关闭 QUIT 从容关闭,关闭主进程及子进程HUP重载配置文件
USR1 重新打开日志文件 USR2 平滑升级可执行程序
# /usr/local/nginx/sbin/nginx -s stop
# elinks--dump http://localhost/a.html
ELinks: 拒绝连接
#
# /usr/local/nginx/sbin/nginx
# elinks--dump http://localhost/a.html
123
#
kill-HUP`/usr/local/nginx/logs/nginx.pid`
elinks--dump http://localhost:8080/a.html
平滑升级nginx
22tar -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
# makeupgrade
/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`
#
主配置文件的语法格式
http{
server {
listen 80;
server_namelocalhost;
location / {
root html;
indexindex.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;
indexindex.html;
}
}
}
-------------------------------------------------------------
基于域名的虚拟主机(通过主机名区分客户端的访问)
http://www.tarena.com /usr/local/nginx/html
http://bbs.tarena.com /bbsdir
192.168.1.254
vim nginxsername.conf
http{
server {
listen80;
server_name www.tarena.com;
location/{
root html;
indexindex.html;
}
}
server {
listen80;
server_name bbs.tarena.com;
location/{
root /bbsdir;
indexindex.html;
}
}
}
:wq
mkdir /bbsdir
echo123 >/bbsdir/index.html
echo567> /usr/local/nginx/html/index.html
客户端 要能够解析主机名到web服务器的Ip地址
vim/etc/hosts
192.168.1.254www.tarena.comwww
192.168.1.254bbs.tarena.combbs
:wq
# elinks--dump http://bbs.tarena.com
123
# elinks--dump http://www.tarena.com
567
#
-----------------------------------------------------------------
基于Ip地址的虚拟主机(通过ip地址区分客户端的访问)
http://1.0.0.254 /usr/local/nginx/html/
http://192.168.1.254 /bbsdir/
catip.conf
http {
include mime.types;
default_typeapplication/octet-stream;
sendfile on;
keepalive_timeout65;
server{
listen192.168.1.254:80;
location / {
root/bbsdir;
index index.html;
}
}
server {
listen 1.0.0.254:80;
location / {
root html;
indexindex.html index.htm;
}
error_page 500 502 503 504/50x.html;
location = /50x.html {
root html;
}
}
:wq
客户访问
ping192.168.1.254
ping1.0.0.254
# elinks--dumphttp://1.0.0.254
567
# elinks--dumphttp://192.168.1.254
123
#
-----------------------------------------------------------------
客户端访问控制 location/{ }
默认允许所有客户端访问网站目录下网页文件
location / {
.....
.....
allow192.168.1.188;
deny all;
}
用户验证 (访问网页文件时,需要提交用户名和 密码)
location /{
.....
.....
allow192.168.1.188;
deny all;
auth_basic"auth-domain";
auth_basic_user_file "/usr/local/nginx/conf/user.txt";}
rpm-q httpd-tools
# htpasswd -c /usr/local/nginx/conf/user.txt webadmin
New password:
Re-type new password:
Adding password for user webadmin
# cat /usr/local/nginx/conf/user.txt
webadmin:.GHgOK5P5MaiY
#
-----------------------------------------------------------------
配置虚拟主机
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反向代理
vimnginx.conf
upstream"webgrp" {
server 192.168.1.1:80 weight=3;
server 192.168.1.2:80;
}
server {
listen80;
location / {
#proxy_passhttp://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;
}
页:
[1]