前端 nginx 配置 与部署配置基础概念

16张图入门nginx

nginx 缓存

nginx 常用配置

nginx 安全配置规范

nginx SLL 安全配置规范

前端nginx部署配置

查找nginx的位置
    
      whiereis nginx
    
  
nginx 启动、停止、重启
    
      # 启动 (选择一个执行即可)
      nginx # nginx直接启动 需要/etc/sbin下有nginx 软连接或者硬连接
      systemctl start nginx.service # systemctl命令启动 使用Linux 的命令进行启动,这种方法无论是启动什么服务都是一样的,只是更改了服务的名字

      # nginx 运行状态
      ps aux | grep nginx

      # 停止 (选择一个执行即可)
      nginx -s stop # 强制停止方法,无论是否在工作
      nginx -s quit # 从容停止服务 这种方法需要进程完成当前工作后再停止
      killall nginx # 停止服务
      systemctl stop nginx.service

      # 重启 (选择一个执行即可)
      nginx -s reload
      systemctl restart nginx.service

      # 查看端口号
      netstat -tlnp

      # 测试配置
      nginx -t
    
  
nginx 配置文件
    
      user  nginx; # 运行用户,默认是nginx,可以不进行设置
      worker_processes  1; # nginx 进程,一般设置为和CPU核数一样 或者两倍
      error_log  /var/log/nginx/error.log warn; # 错误日志存放目录
      pid        /var/run/nginx.pid; # 进程 pid 存放位置

      events {
          ...
      }


      http {
          ...
          # include /etc/nginx/conf.d/*.conf; # 包含的子配置项位置和文件
          server {
            ...
            location / {
                ...
            }
        }
      }
    
  
event
    
      events {
          worker_connections  1024; # 单个后台进程的最大并发数
      }
    
  
http
    
      http {
        include       /etc/nginx/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  /var/log/nginx/access.log  main; # nginx 访问日志存放位置

        sendfile        on; # 开启高效传输模式
        # tcp_nopush     on; # 减少网络报文段的数量
        # keepalive_timeout  0;

        keepalive_timeout  65; # 保持连接的时间,也叫做超时连接

        # gzip  on; # 开始 gzip压缩

        # include /etc/nginx/conf.d/*.conf; # 包含的子配置项位置和文件
        server {
          ...
        }
    }
    
  
server
    
        server {
          # 当nginx接到请求后,会匹配其配置中的service模块
          # 匹配方法就是将请求携带的host和port去跟配置中的server_name和listen相匹配
          listen       80; # 配置监听端口
          server_name  localhost; # 配置域名
      
          # charset koi8-r;
          # access_log  /var/log/nginx/host.access.log  main;
      
          location / {
              # root   html; # Nginx默认值
              root   /usr/share/nginx/html; # 服务默认启动目录(可修改为自己的项目根目录)
              # 设定Nginx服务器返回的文档名
              index  index.html index.htm; # 默认访问文件 先找根目录下的index.html,如果没有再找index.htm

              # 有时候我们的服务器只允许特定主机访问,比如内部OA系统,或者应用的管理后台系统,更或者是某些应用接口,
              # 这时候我们就需要控制一些IP访问,我们可以直接在location里进行配置。
              # deny   123.9.51.42; # 限制访问 拒绝访问
              # allow  45.76.202.231; # 允许访问
          }

          # 上面的配置,表示只允许 45.76.202.231访问,禁止ip地址访问。而如果将 deny语句提前,则会发现所有的ip都不可以访问了。
          # 这说明了:在同一个块中的两个权限指令,先出现的设置为覆盖后出现的设置
          # location / {
          #   allow  45.76.202.231;
          #   deny   all;
          # }

          # 动静分离配置
          # 静态化配置,所有静态请求都转发给 nginx 处理,存放目录为 my-project
          # location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|js|css)$ {
          #     root /usr/local/var/www/my-project; # 静态请求所代理到的根目录
          # }
          # 动态请求匹配到path为'node'的就转发到8002端口处理
          # location /node/ {  
          #     proxy_pass http://localhost:8002; # 充当服务代理 反向代理配置,请求会被转发到8002端口
          # }
      
          # error_page  404              /404.html; # 配置404页面
          # 处理错误的时候,不仅可以只使用本服务器的资源,还可以使用外部的资源。比如我们将配置文件设置成这样。
          # error_page 404 http://laibh.top
          
          # error_page指令用于自定义错误页面,500,502,503,504 这些就是HTTP中最常见的错误码,
          # /50.html用于表示当发生上述指定的任意一个错误的时候,都是用网站根目录下的/50.html文件进行处理。
          # redirect server error pages to the static page /50x.html
          # 
          error_page   500 502 503 504  /50x.html; # 错误状态码的显示页面,配置后需要重新启动
          location = /50x.html {
              root   /usr/share/nginx/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;
          # }
        }
    
  
负载均衡配置
    
      # 负载均衡:设置domain
      upstream domain {
          server localhost:8000;
          server localhost:8001;
      }
      server {  
        listen       8080;        
        server_name  localhost;

        location / {
            # root   html; # Nginx默认值
            # index  index.html index.htm;
            
            proxy_pass http://domain; # 负载均衡配置,请求会被平均分配到8000和8001端口
            proxy_set_header Host $host:$server_port;
        }
      }
    
  

Nginx设置虚拟主机

基于端口号配置虚拟主机
基于IP配置虚拟主机
使用域名设置虚拟主机

Nginx反向代理的设置

最简单的反向代理
    
      server{
        listen 80; # 监听的端口号
        server_name 域名1; 
        location /api {
            proxy_pass 域名2;
        }
      }
    
  
一般我们反向代理的都是一个ip,但是代理域名也是可以的。
其他反向代理指令

Nginx适配PC或移动设备

通过配置Nginx来识别移动端和pc端来展示不同的页面。
$http_user_agent的使用

Nginx的Gzip压缩配置

gzip的配置项
gzip简单配置
    
      http {
        .....
        ### gzip 配置
        # 开启gzip
        gzip on;
        # 启用gzip压缩的最小文件;小于设置值的文件将不会被压缩
        gzip_min_length 1k;
        # gzip 压缩级别 1-10
        gzip_comp_level 2;
        # 进行压缩的文件类型。
        gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
        # 是否在http header中添加Vary: Accept-Encoding,建议开启
        gzip_vary on;
        .....
     }
    
  

Nginx的location、root、alias指令用法和区别

nginx指定文件路径有两种方式root和alias,指令的使用方法和作用域: