• Nginx的简单使用


    一、Nginx概述

      Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。它的特点是支持高并发;资源消耗少;可以做HTTP反向代理和负载均衡;支持异步网络i/o事件模型epoll。

    (一)Nginx目录文件

    [root@bogon nginx1-12]# ls
    client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp

    下面列出几个比较重要的目录功能:

    • conf 存放nginx所有配置文件的目录,主要nginx.conf
    • html 存放nginx默认站点的目录,如index.html、error.html等
    • logs 存放nginx默认日志的目录,如error.log access.log
    • sbin 存放nginx主命令的目录,sbin/nginx

    Nginx主配置文件nginx.conf解析:

    #定义Nginx运行的用户
    #user  nobody;
    
    #Nginx进程数,一般为CPU的核数
    worker_processes  1;
    
    #全局错误日志定义类型
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #进程pid文件
    #pid        logs/nginx.pid;
    
    
    #事件模型,配置耽搁进程最大连接数
    events {
        worker_connections  1024;
    }
    
    #设定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指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。
    如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。
    sendfile on; #tcp_nopush on; #超时时间 #keepalive_timeout 0; keepalive_timeout 65; #开启gzip压缩输出 #gzip on; #虚拟主机的配置 server { #监听端口 listen 80; #域名可以有多个,用空格隔开 server_name localhost; #charset koi8-r; #定义本虚拟主机的访问日志 #access_log logs/host.access.log main; #控制网站访问路径 location / { root html; #存放网站代码路径 index index.html index.htm; #服务器返回的默认页面文件 } # 指定错误代码, 统一定义错误页面, 错误代码重定向到新的Locaiton #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } #启用反向代理 # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ .php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ .php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /.ht { # deny all; #} } #第二个虚拟主机配置 # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }

    (二)部署访问站点

    这里是nginx已经安装完成了,接着就是启动nginx,进入安装目录的sbin目录下:

    [root@bogon nginx1-12]# ls
    client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
    [root@bogon nginx1-12]# cd sbin/  #进入sbin目录
    [root@bogon sbin]# ls
    nginx
    [root@bogon sbin]# ./nginx  #启动nginx
    [root@bogon sbin]# ./nginx -s stop #关闭nginx
    [root@bogon sbin]# ./nginx -s reload #重新加载

    启动完毕后,可以进行检测是否启动成功:

    [root@bogon ~]# netstat -tunlp |grep 80
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      3796/nginx: master  
    [root@bogon ~]# curl -I 127.0.0.1
    HTTP/1.1 200 OK
    Server: nginx/1.12.0
    Date: Thu, 17 Oct 2019 04:03:18 GMT
    Content-Type: text/html
    Content-Length: 612
    Last-Modified: Tue, 04 Jun 2019 09:48:32 GMT
    Connection: keep-alive
    ETag: "5cf63e70-264"
    Accept-Ranges: bytes

    显然,已经启动成功,并且可以正常访问。

    (二)基于域名的多虚拟主机实战

      如果一台linux服务器只运行了一个小网站并且网站的流量小,这样站长可能会承担高额的服务器租赁费以及造成了硬件资源的浪费。为了解决这个问题引入了虚拟主机

      虚拟主机(英语:virtual hosting)或称 共享主机(shared web hosting),又称虚拟服务器,是一种在单一主机或主机群上,实现多网域服务的方法,可以运行多个网站或服务的技术。

      其实虚拟主机就是将一台服务器分割成多个“虚拟服务器”,每个站点使用各自的硬盘空间,由于省资源,省钱,众多网站都使用虚拟主机来部署网站。

      Nginx支持多虚拟机的操作,在nginx.conf中使用server{}代码块标签来表示一个虚拟主机。Nginx支持多个server{}标签,即支持多个虚拟主机站点。

    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;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        #gzip  on;
        #第一个虚拟主机
        server {
            listen  80;
            # server_name  localhost;
             server_name www.nginxhs1.com;
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                    root   html;
                    index  index.html index.htm;
            }
    
            #error_page  404              /404.html;
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
       
       #第二个虚拟主机 
        server {
            listen  80;
            # server_name  localhost;
             server_name www.nginxhs2.com;
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                    root   html;
                    index  index.html index.htm;
            }
    
            #error_page  404              /404.html;
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
    ...
    
    }

      每一个server{}就是一个虚拟主机,在内部可以配置站点信息,其中server_name名称需要自己在本地的hosts文件中进行ip与域名的映射,windows(位于C:WindowsSystem32driversetc)中进行设置:

    127.0.0.1 localhost
    
    192.168.0.104 www.yw.com   #linux系统上的ip
    
    192.168.0.104 www.nginxhs.com

    二、主要功能

    (一)反向代理

       正向代理:是针对客户端来讲的,比如一个网址客户端是访问不了,但是可以访问代理服务器,然后代理服务器替代我们去访问原始服务器拿到资源再返回给我们。所以正向代理就是位于客户端与原始服务器之间的服务器,去代替我们访问一些我们访问不了的服务器。

      反向代理:是针对服务器来讲的,用于接收客户端的请求,并且将请求再转发给具体的服务器进行处理请求,然后再将服务器的响应结果反馈给客户端。

      比如在linux系统上部署nginx+uwsgi+django项目,此时需要nginx反向代理进行转发,nginx接收到http请求后将其转发给uwsgi,uwsgi 接收到请求之后将包进行处理,处理成wsgi可以接受的格式,并发给wsgi,wsgi 根据请求调用应用程序的某个文件,某个文件的某个函数,最后处理完将返回值再次交给wsgi,wsgi将返回值进行打包,打包成uwsgi能够接收的格式,uwsgi接收wsgi 发送的请求,并转发给nginx,nginx最终将返回值返回给浏览器。

    所以在nginx.conf中就需要进行配置反向代理:

      可以看到反向代理使用的是uwsgi_pass参数而非proxy_pass参数,proxy_pass配置走的是http协议,uwsgi_pass未指出协议走的是socket,这样就完成了请求到达nginx的80端口后转发给后面django程序启动的8000端口。

    (二)负载均衡

    多在高并发情况下需要使用。其原理就是将数据流量分摊到多个服务器执行,减轻每台服务器的压力,多台服务器(集群)共同完成工作任务,从而提高了数据的吞吐量。

    Nginx可使用的负载均衡策略有:轮询(默认)、权重、ip_hash、url_hash(第三方)、fair(第三方)。

    worker_processes  1;
    events {
        worker_connections  1024;
    }
    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;
        keepalive_timeout  65;
    #定义负载均衡池,名字叫做yw,池子中写入uwsgi发布yw的socket地址 upstream yw{ server 0.0.0.0:8000 weight=10; #根据权重大小轮询转发到此处 } server { listen 80; server_name www.yw.com; #访问nginx的根路径时,转发请求给uwsgi的8000端口,这里要和uwsgi.ini写的一致 location / { include /root/Env/yw_crm_project/nginx/conf/uwsgi_params; #这里是导入的uwsgi配置 uwsgi_pass yw; #请求转发给upstream地址池里的uwsgi程序,yw就是负载均衡池的名字

    } location /static/ { alias /root/Envs/yw_crm_project/nginx/static/; #处理静态页面资源 } } }

      定义了负载均衡池yw,内部放着很多的主机(服务器),nginx根据uwsgi_pass参数进行转发给负载均衡池中的某个服务器,而服务器的选择是根据nginx默认的轮询进行的,当然你可以对每一个服务器设置权重,这样被轮询到的概率就会增大。

      另外,可以看到nginx进行了动静分离,将静态资源直接放到nginx上,动态请求转发到uwsgi服务器进行处理。这样使整个服务器系统的性能、效率更高。

  • 相关阅读:
    ubuntu 20.04中配置静态固定IP
    R语言中安装包常见的3种来源
    centos 7.9.2009中安装 R、Rstudioserver
    ubuntu 20.04 中安装Rstudioserver
    Centos7 安装R语言并安装R包
    linux 中netstat命令
    十六进制颜色对照表
    Cesium 获取当前场景坐标范围
    使用ArcMap中的工具时,总是弹出windows安全警告窗口解决方法_会握爪的小金毛的博客
    Cesium 解决两个billboard重叠问题_easyCesium的博客
  • 原文地址:https://www.cnblogs.com/shenjianping/p/11632615.html
Copyright © 2020-2023  润新知