搭建Nginx

2、作为web服务器

相比apache,nginx使用更少的资源,支持更多的并发连接,体现更高的效率。

Nginx作为负载均衡服务器:nginx既可以在内部直接支持rails和php程序对外进行服务,也可以支持http代理服务器对外进行服务。Nginx采用C进行编写,不论是系统资源开销还是CPU使用效率都比较好。

作为邮件代理服务器:最早开发这个产品的目的之一也是作为邮件代理服务器。

3、nginx配置简洁,apache较复杂

Nginx静态处理性能比apache高3倍以上

4、最核心的区别在于

apache 是同步多进程模型,一个连接对应一个进程,Nginx是异步的,多个连接可以对应一个进程。

Nginx处理静态文件好,耗费内存少,只适合静态和反向。Apache在处理动态有优势,nginx并发性比较好,CPU占用内存低,如果rewrite频繁, 选用apache最佳。

总的来说,apache依然是大部分公司的首选

二、Nginx 服务基础

1.Nginx 安装及运行控制

1.编译安装Nginx

1.关闭防火墙,将安装nginx所需软件包传到/opt目录下

 

2.安装依赖包

nginx的配置及运行需要pcre、zlib等软件包的支持,因此需要安装这些软件的开发包,以便提供相应的库和头文件。

yum -y install pcre-devel zlib-devel gcc gcc-c++ make

 3.创建运行用户、组(Nginx服务程序默认以nobody身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限)

4.编译安装Nginx

cd /opt/nginx-1.12.0/

./configure \
--prefix=/usr/local/nginx \                        #指定nginx的安装路径
--user=nginx \                                     #指定用户名
--group=nginx \                                    #指定组名
--with-http_stub_status_module                     #启用http_stub_status_module模块以支持状态统计

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module

make -j8 && make install

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/          #让系统识别nginx的操作命令



 5.检查、启动、重启、停止Nginx服务

nginx -t
#检查配置文件是否正确

nginx
##启动

##停止
cat /usr/local/nginx/logs/nginx.pid
#先查看nginx的PID号
kill -3 <PID号>
kill -s QUIT <PID号>
killall -3 nginx
killall -s QUIT nginx 

##重载
kill -1 <PID号>
kill -s HUP <PID号>
killall -1 nginx
killall -s HUP nginx

##日志分隔,重新打开日志文件
kill -USR1 <PID号>

##平滑升级
kill -USR2 <PID号>

  1. 添加 Nginx 系统服务
vim /etc/init.d/nginx

#!/bin/bash
#chkconfig: 35 99 20
#description:Nginx Service Control Script

COM="/usr/local/nginx/sbin/nginx"
PID="/usr/local/nginx/logs/nginx.pid"
case "$1" in

start)
$COM
;;

stop)
kill -s QUIT $(cat $PID)
;;

restart)
$0 stop
$0 start
;;

reload)
kill -s HUP $(cat $PID)
;;

*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1

esac
exit 0


chmod +x /etc/init.d/nginx
chkconfig --add nginx                        #添加为系统服务
systemctl stop nginx
systemctl start nginx

 

方法二

vim /lib/systemd/system/nginx.service

[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecrReload=/bin/kill -s HUP $MAINPID
ExecrStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service

 2.配置文件nginx.conf

vim /usr/local/nginx/conf/nginx.conf

 1. 全局配置

#user nobody;                   #运行用户,若编译时未指定则默认为nobody
worker_ processes 1;            #工作进程数量,可配置成服务器内核数* 2,如果网站访问量不大,一般设为1就够用了
#error__log logs/error.log;     #错误日志文件的位置
#pid logs/nginx.pid;            #PID文件的位置.

2. I/O 事件配置

events {
use epoll;                #使用epoll模型,2.6及以上版本的系统内核,建议使用epoll模型以提高性能
worker_connections 1024;  #每个进程处理4096个连接
}

 

 3.HTTP配置

http {
##文件扩展名与文件类型映射表
include         mime.types;

##默认文件类型
default_ type application/octet-stream;

##日志格式设定
#log_ format  main    '$remote_addr 一 $remote_user [$time_local] "$request" '
#                     '$status $body_bytes_sent" $http_referer"
#                     '"$http_user_agent" "$http_x_forwarded_for"';

##访问日志位置
#access_log logs/access.log main;

##支持文件发送(下载)
sendfile     on;

##此选项允许或禁止使用socket的TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用
#tcp_nopush       no;

##连接保持超时时间,单位是秒
#keepalive_timeout  0;
keepalive_timeout  65;

##gzip模块设置,设置是否开启gzip压缩输出
#gzip    on;


Web服务的监听配置

server {

listen 80;                ##.监听地址及端口
server_name www.lp.com;   ##站点域名,可以有多个,用空格隔开
charset utf-8;            ##网页的默认字符集

location / {                     ##根目录配置
root html;                       ##网站根目录的位置/usr/local/nginx/html
index index.html index. php;     ##默认首页文件名
}

error_page 500 502 503 504 /50x.html;     ##内部错误的反馈页面
location = /50x.html {                    ##错误页面配置
root html ;
 }
 }
 }

 3.访问状态统计配置

1.先使用命令/usr/local/nginx/sbin/nginx -V查看已安装的Nginx 是否包含HTTP_STUB_STATUS模块
2.修改nginx.conf配置文件,指定访问位置并添加stub_status配置

cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.bak
vim /usr/local/nginx/conf/nginx.conf

 3.访问状态统计

1.先使用命令/usr/local/nginx/sbin/nginx -v查看已安装的Nginx是否包含HTTP_STUB_STATUS模块

2.修改nginx.conf配置文件,指定访问位置并添加stub_status配置

cd /usr/local/nginx/conf
cp nginx.conf{,.bak}

vim /usr/local/nginx/conf/nginx.conf
.....
http {
.....
  server {
    listen 80;
    server_name www.xcf.com;
    charset utf-8;
    location / {
      root html;
      index index.html index.html;
    }
    ##添加stub_status 配置##
    location /status {      #访问位置为/status
      stub_status on;       #打开状态统计功能
      access_log off;       #关闭此位置的日志记录
    }
  }
}

 二、Nginx访问控制

1.生成用户密码认证文件

yum install -y httpd-tools

htpasswd -c /usr/local/nginx/passwd.db zhangsan

chown nginx /usr/local/nginx/passwd.db
chmod 400 /usr/local/nginx/passwd.db

2.修改主配置文件相对应目录,添加认证配置项

相当于白名单与黑名单
访问控制规则如下:
deny IP/IP 段:拒绝某个 IP 或 IP 段的客户端访问
allow IP/IP 段:允许某个 IP 或 IP 段的客户端访问
规则从上往下执行,如匹配则停止,不再往下匹配

vim /usr/local/nginx/conf/nginx.conf
......
    server {
        location / {
        ......
        ##添加控制规则##
        deny 192.168.126.100;
        #拒绝访问的客户端IP
        allow all;
        #允许其它IP客户端访问
        }
    }
    
systemctl restart nginx

 三、Nginx虚拟主机

1.基于域名的虚拟主机

1.为虚拟主机提供域名解析

2.为虚拟主机准备网页文档

3.修改Nginx的配置文件

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
    server {
        listen 80;
        server_name www.xcf.com;
        #设置域名www.xcf.com
        charset utf-8;
        access_log logs/www.xcf.com.access.log;
        #设置日志名
        location / {
            root /var/www/html/xcf;
            #设置www.xcf.com的工作目录
            index index.html index.html;
        }
        error_page 500 502 503 504 /50x.html;
        location = 50x.html{
            root html;
        }
    }

##以上模板完成后复制黏贴并进行简单修改即可
    server {
        listen 80;
        server_name www.zxc.com;
        #设置域名www.zxc.com
        charset utf-8;
        access_log logs/www.zxc.access.com.log;
        location / {
        root /var/www/html/zxc;
        index index.html index.html;
        }
        error_page 500 502 503 504 /50x.html;
        location = 50x.html{
            root html;
        }
    }
}
nginx -t
systemctl restart nginx.service

 2.基于IP的虚拟主机

ifconfig ens33:0 192.168.126.65 netmask 255.255.255.0
ifconfig


vim /usr/local/nginx/conf/nginx.conf
......
http {
......
    server {
        listen 192.168.126.15:80;
        #设置监听地址192.168.126.15
        server_name www.xcf.com;
        charset utf-8;
        access_log logs/www.xcf.access.log;
        location / {
            root /var/www/html/xcf;
            index index.html index.php;
        }
        error_page 500 502 503 504 /50x.html;
        location = 50x.html{
            root html;
        }
    }
    
    server {
        listen 192.168.126.65:80;
        #设置监听地址192.168.126.65
        server_name www.zxc.com;
        charset utf-8;
        access_log logs/www.zxc.access.log;
        location / {
            root /var/www/html/zxc;
            index index.html index.html;
        }
        error.page 500 502. 503 504 /50x.html;
        location = 50x.html{
            root html;
        }
    }
}

nginx -t
systemctl restart nginx

浏览器访问
http://192.168.102.136
http://192.168.102.136

3.基于端口的虚拟主机

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
    server {
        listen 192.168.126.15:8080;
        #设置监听8080 端口
        server_name www.xcf.com;
        charset utf-8;
        access_log logs/www.xcf.access.log;
        location / {
            root /var/www/html/xcf;
            index index.html index.html;
        }
        error_page 500 502. 503 504 /50x.html;
        location = 50x.html{
            root html;
        }
    }

    server {
        listen 192.168.126.65:8888;
        #设置监听8888端口
        server_name www.zxc.com;
        charset utf-8;
        access_log logs/www.zxc.access.log;
        location / {
            root /var/www/html/benet;
            index index.html index.php; 
        }
        error_page 500 502 503 504 /50x.html;
        location = 50x.html{
            root html;
        }
    }
}

nginx -t
systemctl restart nginx.service


浏览器访问
http://192.168.102.136:8080
http://192.128.102.136:8888

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值