美文网首页
nginx学习笔记

nginx学习笔记

作者: dounine | 来源:发表于2020-01-20 09:22 被阅读0次

    0. 前言

    关于nginx相关操作的笔记

    1. 安装

    许多博客上提供的安装方法特别复杂,不知为何。

    centos上,我一般都是:

    yum install nginx
    

    (注意,centos上许多工具都依托于python2,如果命令python指向了python3,则在使用大多数命令时会报错)

    2. nginx相关命令

    $ nginx -h
    nginx version: nginx/1.12.2
    Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
    
    Options:
      -?,-h         : this help
      -v            : show version and exit
      -V            : show version and configure options then exit
      -t            : test configuration and exit
      -T            : test configuration, dump it and exit
      -q            : suppress non-error messages during configuration testing
      -s signal     : send signal to a master process: stop, quit, reopen, reload
      -p prefix     : set prefix path (default: /usr/share/nginx/)
      -c filename   : set configuration file (default: /etc/nginx/nginx.conf)
      -g directives : set global directives out of configuration file
    

    常用的有:

    nginx -s stop # 停止nginx
    nginx -s reload # 重启
    nginx -c filename # 以filename为配置文件启动,默认的配置文件在 /etc/nginx/nginx.conf
    

    3. 修改nginx所属用户

    nginx.conf配置文件的根属性user修改为root

    防止出现权限问题

    4. 开启一个http服务器

    在根属性http{}中添加:

    server {
        listen       8001; # 监听的端口
        server_name  tiger; # 服务器名字
        root         /home/test/tiger/; # 允许访问的静态文件存放路径
    
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
    
        # 默认访问文件
        location / {
            index index.html;
        }
    
        error_page 404 /404.html;
            location = /40x.html {
        }
    
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
    

    配置完,通过nginx -s reload或者nginx -c <filename>启动。

    然后,就可以通过服务器ip地址:8001或者服务器域名:8001访问了。

    5. 反向代理(解决跨域问题)

    所谓反向代理是指在服务器端进行设置代理:客户端访问的目标网站实际上是一个代理;

    正向代理则是指在客户端设置代理,客户以代理ip身份去访问目标网站。例如,先开代理,然后翻墙访问pxxxhup。

    nginx设置反向代理,在http{ server{}}中添加:

    server {
        listen       8000;
        server_name  _; # 匿名,没什么问题
        root         /home/test/myblog_Fontend/;
    
        ....
    
        # 所有访问 ip:8000/api 的请求,都将转到 http://0.0.0.0:8081,解决跨域问题的一剂良方
        location /api {
            include  uwsgi_params;
            proxy_pass   http://0.0.0.0:8081;
        }
    }
    

    6. gzip压缩文件

    此方法可以提高网站访问速度

    http{}中添加:

    #开启和关闭gzip模式
    gzip on;
    #gizp压缩起点,文件大于1k才进行压缩
    gzip_min_length 1k;
    # gzip 压缩级别,1-9,数字越大压缩的越好,也越占用CPU时间
    gzip_comp_level 6;
    # 进行压缩的文件类型。
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript ;
    #nginx对于静态文件的处理模块,开启后会寻找以.gz结尾的文件,直接返回,不会占用cpu进行压缩,如果找不到则不进行压缩
    gzip_static on;
    # 是否在http header中添加Vary: Accept-Encoding,建议开启
    gzip_vary off;
    # 设置gzip压缩针对的HTTP协议版本
    gzip_http_version 1.1;
    

    相关文章

      网友评论

          本文标题:nginx学习笔记

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