zoukankan      html  css  js  c++  java
  • Linux之 Ngnix

    前言:

    WEB框架

    django 大而全, 功能特别多 form表单 , ORM, 中间件 笨重,臃肿 600/s

    flask 轻量级的,小而精, 它使用的都是第三方模块进行拼接起来的 4988/s

    tornado 支持异步, 处理用户请求过来数据不用等待,类似于协程 2138/s

    sanic python3.5+uvloop 33342/s

     

    WEB服务器

    nginx 开源的,支持高性能,高并发的

    apache nginx他父亲 (apache不支持高并发)

    IIS(windows下面的WEB Server)

     

    Nginx安装步骤 (编译安装)

    1、安装nginx需要的依赖库
    yum install -y gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
    
    2、下载安装nginx源码包
    cd /opt
    wget -c https://nginx.org/download/nginx-1.12.0.tar.gz
    
    3、解压源码
    tar -zxvf nginx-1.12.0.tar.gz  
    
    4、配置,编译安装,开启nginx状态监测功能
    
    cd /opt/nginx-1.12.0
    ./configure --prefix=/opt/nginx112
    
    5 编译安装
    
    cd /opt/nginx-1.12.0
    make && make install
    
    6、启动nginx,进入sbin目录,执行nginx文件
    
    cd /opt/nginx112/sbin
    ./nginx #启动
    ./nginx -s stop #关闭
    ./nginx -s reload  # 平滑重启 ,修改了nginx.conf之后,可以不重启服务,加载新的配置
    
    7、查看nginx运行状态
    查看端口是否运行: netstat -tunlp
    查看进程是否运行: ps -ef | grep nginx
    
    8 nginx目录下的文件
    
    conf 存放nginx所有配置文件的目录,主要nginx.conf
    html 存放nginx默认站点的目录,如index.html、error.html等
    logs 存放nginx默认日志的目录,如error.log access.log
    sbin 存放nginx主命令的目录,sbin/nginx

    nginx.conf 配置文件解析( nginx/conf/nginx.conf )

     配置文件详解

    # 配置文件详解
    
    #定义nginx工作进程数
    worker_processes  5;
    #错误日志
    #error_log  logs/error.log;
    #http定义代码主区域
    http {
        include       mime.types;
        default_type  application/octet-stream;
        #定义nginx的访问日志功能
        #nginx会有一个accses.log功能,查看用户访问的记录
        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;
        keepalive_timeout  65;
        #开启gzip压缩传输
        gzip  on;
        #虚拟主机1  定义一个 斗鱼网站  将一个server代表一个网站,可以写多个server
        server {
            #定义nginx的访问入口端口,访问地址是  192.168.11.37:80
            listen       80;
            #定义网站的域名www.woshidouyu.tv
            #如果没有域名,就填写服务器的ip地址  192.168.11.37
            server_name  www.woshidouyu.tv;
            #nginx的url域名匹配
            #只要请求来自于www.woshidouyu.tv/111111111
            #只要请求来自于www.woshidouyu.tv/qweqwewqe
            #最低级的匹配,只要来自于www.woshidouyu.tv这个域名,都会走到这个location
            location / {
                #这个root参数,也是关键字,定义网页的根目录
                #以nginx安装的目录为相对路径  /opt/nginx112/html 
                #可以自由修改这个root定义的网页根目录
                root   html;
                #index参数定义网站的首页文件名,默认的文件名
                index  index.html index.htm;
            }
            #错误页面的优化(只要是遇到前面4系列的错误,就会直接跳转到相对目录下的40x.html页面)
            error_page  400 401  402  403  404   /40x.html;
        }
      server {   #  第二个主页  可以加多个主页一个server{} 代表一个主页
            listen       80;
            server_name  www.echo.com;
            location / { 
                root   /opt/nginx/html;
    
                index  echo.html echo.htm;
            }
            #错误页面的优化(只要是遇到前面4系列的错误,就会直接跳转到相对目录下的40x.html页面)
            error_page  400 401  402  403  404   /40x.html;
        }
    }

    nginx错误页面      nginx.conf文件

    vim /opt/nginx112/conf/nginx.conf
    修改server代码块
    server {
            listen       80;
            server_name  localhost;
            location / {
                root   html;
                index  index.html index.htm;
            }
            error_page   400 402 403 404  /40x.html;
            #error_page   500 502 503 504  /50x.html;
        }
    
    
    
    
    平滑重启nginx
    /opt/nginx112/sbin/nginx -s reload

    nginx访问日志功能

    默认是注释的,把#号去掉就行
    
    #定义nginx的访问日志功能
        #nginx会有一个accses.log功能,查看用户访问的记录
        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; 

    nginx限制网站来源IP访问

     

     如果想要在本地访问一个域名, 可以更改本机hosts文件

     

    Nginx代理功能

    1 修改代理服务器192.168.1.15的配置文件

    vim /opt/nginx112/conf/nginx.conf
    在location代码块下添加一行数据
    proxy_pass http://192.168.1.40;
    # 经过测试
    1、一台服务器的nginx.conf配置文件中,如果有多个server,只要在一个server中设置了代理,那么访问那台服务器的其他server也会被代理跳转
    
    2、如果被跳转的服务器,有多个server,则跳转后会显示第一个server的主页

    Nginx负载均衡

    代理服务器配置如下:

    如果代理服务器有多个server
    
    1、负载必须配置到第一个server的上面,否则每次访问只会返回第一个server的页面
    
    2、第一个server配置了负载,其他server 用域名可以访问

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    macbook 无声音解决方案
    webapck dev server代理请求 json截断问题
    百度卫星地图开启
    服务器 nginx配置 防止其他域名绑定自己的服务器
    记一次nginx php配置的心路历程
    遇到npm报错read ECONNRESET怎么办
    运行svn tortoiseSvn cleanup 命令失败的解决办法
    svn add 命令 递归目录下所有文件
    m4出现Please port gnulib freadahead.c to your platform! Look at the definition of fflush, fread, ungetc on your system, then report this to bug-gnulib."
    Ubuntu下安装GCC,mpc、mpfr、gmp
  • 原文地址:https://www.cnblogs.com/echo2019/p/10643160.html
Copyright © 2011-2022 走看看