美文网首页
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学习笔记-基本操作

    title: Nginx学习笔记-基本操作date: 2018-07-08tags: [nginx]categor...

  • Nginx学习笔记-项目部署

    title: Nginx学习笔记-项目部署date: 2018-08-18tags: [nginx]categor...

  • Nginx学习笔记

    Nginx学习笔记 目录 yum安装nginx 本地编译安装nginx niginx配置文件说明 1.yum安装n...

  • Nginx安装使用教程

    一、Nginx代理转发到应用服务1、 Nginx入门学习笔记Windows系统:1、到nginx官网下载,解压即可...

  • Nginx

    Nginx学习笔记 1、Nginx初识 Nginx 是一个高性能的Http和反向代理服务器。也是一个IMAP/PO...

  • Nginx学习笔记

    1.什么是负载均衡?通过某种负载分担技术,将外部发送来的请求按照某种策略分配到服务器集合的某一台服务器上,而接收到...

  • nginx学习笔记

    一、常见服务器 Apache \ IIS \ Tomcat \ Lighttpd \ Nginx 二、nginx....

  • Nginx学习笔记

    基于《实战Nginx:取代Apache的高性能Web服务器》 2010年版本 第一章 Nginx简介 1.Ngin...

  • Nginx学习笔记

    Nginx在运行时有一个主进程和多个worker进程,主进程负责总体管理等工作,比如重新加载配置文件以及管理wor...

  • Nginx学习笔记

    nginx的启动参数 例子: 检查配置文件是否正确,只做语法检查,并不进行测试. nginx -t -c 启动服务...

网友评论

      本文标题:nginx学习笔记

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