美文网首页
Nginx学习笔记(一)

Nginx学习笔记(一)

作者: 莫须有的呓语 | 来源:发表于2020-04-01 11:45 被阅读0次

    本篇内容:概述,安装,常用命令,配置文件,demo

    一、nginx概述

    1.1 常用的web服务器

    服务器介绍

    Web 服务器分为静态服务器和动态服务器,静态服务器就是处理静态资源的,比如 HTML、CSS、JS,常用的静态服务器有 Apache、Nginx;动态服务器就是处理动态请求的,比如 JSP,Servlet 等,常用的有 Tomcat、Weblogic。

    Nginx 是一个高性能的 HTTP 服务器和反向代理服务器,能够支持 5 万个并发连接,内存、CPU 消耗非常低,是基于七层协议的服务。

    反向代理

    正向代理:代理客户端,即代替客户端与服务器进行通信,服务器将不知道真实的客户端是谁,比如翻墙

    反向代理:代理服务端,即代替服务器与客户端进行通信,客户端将不知道真实的服务器是哪台,即nginx

    1.2 nginx的原生功能

    1.http服务器。Nginx是一个http服务可以独立提供http服务。可以做网页静态服务器

    2.虚拟主机。可以实现在一台服务器虚拟出多个网站,例如个人网站使用的虚拟机。

    3.反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用nginx做反向代理。并且多台服务器可以平均分担负载,不会应为某台服务器负载高宕机而某台服务器闲置的情况。

    4.正向代理

    二、安装与基础命令

    2.1通过yum安装(含组件)

    系统环境:centOS7.7

    Nginx版本:nginx-1.14.0.tar.gz和nginx-1.16.1.tar.gz均可使用

    1)gcc

    用来编译下载下来的nginx源码

    
    yum install gcc-c++
    
    

    2)pcre和pcre-devel

    PCRE是一个prel库,包括perl兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式; pcre-devel是使用pcre开发的一个二次开发库。

    
    yum install -y zlib zlib-devel
    
    

    3)zlib

    zlib提供了很多压缩和解方式,nginx需要zlib对http进行gzip

    
    yum install -y zlib zlib-devel
    
    

    4)openssl

    openssl是一个安全套接字层密码库,nginx要支持https,需要使用openssl

    
    yum install -y openssl openssl-devel
    
    

    5)下载nginx

    
    wget http://nginx.org/download/nginx-1.14.0.tar.gz
    
    

    6)解压nginx

    
    tar -zxvf nginx-1.14.0.tar.gz -C  /usr/local
    
    

    7)cd到文件路径

    cd /usr/local/nginx-1.14.0
    

    8)编译nginx

    简单版

    ./configure
    

    复杂版:

    ./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/tem/nginx/client --http-proxy-temp-path=/var/tem/nginx/proxy --http-fastcgi-temp-path=/var/tem/nginx/fcgi --with-http_stub_status_module
    

    9)安装nginx

    make && make install
    

    10)启动nginx

    nginx -c /etc/nginx/nginx.conf
    

    问题处理

    ①如果出现[emerg] getpwnam("nginx") failed 错误 执行

    useradd -s /sbin/nologin -M nginx
    
    id nginx
    

    ②如果出现 [emerg] mkdir() "/var/temp/nginx/client" failed (2: No such file or directory) 错误 执行

    sudo mkdir -p /var/tem/nginx/client
    

    ③如果您正在运行防火墙,请运行以下命令以允许HTTP和HTTPS通信:

    sudo firewall-cmd --permanent --zone=public --add-service=http
    
    sudo firewall-cmd --permanent --zone=public --add-service=https
    
    sudo firewall-cmd --reload
    

    11)开机自启动

    在rc.local增加启动代码即可

    vi /etc/rc.local
    

    增加一行

    /usr/local/nginx/sbin/nginx
    

    2.2常用命令

    cd /usr/local/nginx/  进入nginx的位置
    
    nginx  启动nginx
    
    nginx -s reload 重新加载(一般用于修改配置文件后)
    
    nginx -s quit  此方式停止步骤是待nginx进程处理任务完毕进行停止。
    
    nginx -s stop  此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
    

    2.3卸载nginx

    1)停止nginx进程

    nginx -s stop
    

    2)查找nginx相关文件

    find / -name nginx
    
    查找Nginx相关文件

    3)依次删除find查找到的所有目录

    比如:

    rm -rf /usr/sbin/nginx
    

    4)使用yum清理

    yum remove nginx
    

    三、配置文件

    3.1最简改配置

    仅最简单示范代理springboot项目

    springboot项目中推荐做如下配置:

    server.port=8081
    
    server.servlet.context-path=/xx
    

    其中server.servlet.context-path属性应每个模块都唯一,以便于nginx做分发

    访问地址:http://IP:80/xx/请求地址

    nginx配置文件修改部分如下:(配置文件在 /etc/nginx 下)

    
    server {
    
            listen 80;
    
            server_name localhost;   
    
            location / {
    
                    proxy pass http://101.200.147.88:8080;
    
                    #root  html;
    
        #index  index.html index.htm;
    
            }
    
            location /xx {
    
    proxy_pass http://localhost:8081;
    
    }
    
    }
    
    

    3.2原始配置文件

    主要分为三部分

    • 全局块

      设置影响nginx服务器整体运行的配置指令

    • events

      设置网络相关

    • http 块

    配置具体关系

    • http全局块

    • server块

    汉字均为笔者添加

    
    #user  nobody;      #配置用户或者组
    
    worker_processes  1;    #允许生成的进程数,推荐为cpu总核心数
    
    #全局错误日志文件。日志输出级别有debug、info、notice、warn、error、crit(由详细->简略)
    
    #error_log  logs/error.log; 
    
    #error_log  logs/error.log  notice;
    
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;  #指定nginx进程运行文件存放地址
    
    #工作模式及连接数上限
    
    events {
    
        worker_connections  1024; #单个后台 worker process 进程的最大并发链接
    
    数 (最大连接数=连接数*进程数)
    
    }
    
    #设定 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;  #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来 输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置 为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常 把这个改成off。
    
        #tcp_nopush    on;  #防止网络阻塞
    
        #keepalive_timeout  0;
    
        keepalive_timeout  65;  #长连接超时时间,单位是秒
    
        #gzip  on;  #开启gzip压缩输出
    
        server {
    
            listen      80;
    
            server_name  localhost;
    
            #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;
    
            }
    
            # 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;
    
        #    }
    
        #}
    
    }
    
    

    四、demo

    自己写个springboot,按照前面的配置搞一下就好!

    相关文章

      网友评论

          本文标题:Nginx学习笔记(一)

          本文链接:https://www.haomeiwen.com/subject/srjouhtx.html