• Nginx 常用的基础配置(前端相关方面)


    在这里插入图片描述

    Nginx是一款高性能的Web服务器和反向代理服务器,广泛应用于互联网领域。作为一名前端同学,了解并掌握Nginx的配置是非常有必要的。

    安装Nginx

    sudo apt-get update
    sudo apt-get install nginx
    
    • 1
    • 2

    查看Nginx版本

    nginx -v
    
    • 1

    启动、停止、重启Nginx服务

    sudo service nginx start      # 启动Nginx服务
    sudo service nginx stop       # 停止Nginx服务
    sudo service nginx restart    # 重启Nginx服务
    
    • 1
    • 2
    • 3

    Nginx配置文件结构
    Nginx的主配置文件位于/etc/nginx/nginx.conf,其他配置文件位于/etc/nginx/sites-available目录下。每个站点的配置文件以站点名命名,如default、example.com等。
    在这里插入图片描述

    server {
        listen 80;          # 监听端口号
        server_name example.com; # 域名或IP地址
        root /var/www/html; # 网站根目录
        index index.html;   # 默认首页文件名
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    创建站点配置文件
    在/etc/nginx/sites-available目录下创建一个名为example.com的文件,并将上述配置内容复制到该文件中。然后创建一个符号链接到/etc/nginx/sites-enabled目录下:

    sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
    
    • 1

    重启Nginx服务使配置生效

    sudo service nginx restart
    
    • 1

    至此,你已经成功配置了一个基本的Nginx站点。当然,Nginx还有很多高级功能,如负载均衡、缓存、SSL等,可以根据需要进行配置。

    Nginx配置示例

    基础配置:

    user                            root;
    worker_processes                1;
    
    events {
      worker_connections            10240;
    }
    
    http {
      log_format                    '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"';
      include                       mime.types;
      default_type                  application/octet-stream;
      sendfile                      on;
      #autoindex                    on;
      #autoindex_exact_size         off;
      autoindex_localtime           on;
      keepalive_timeout             65;
      gzip                          on;
      gzip_disable                  "msie6";
      gzip_min_length               100;
      gzip_buffers                  4 16k;
      gzip_comp_level               1;
      gzip_types                  text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
      gzip_types                    "*";
      gzip_vary                     off;
      server_tokens                 off;
      client_max_body_size          200m;
    
      server {
        listen                      80 default_server;
        server_name                 _;
        return                      403 /www/403/index.html;
      }
    
      include                       ../serve/*.conf;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    隐藏 Nginx 版本信息:

    http {
      server_tokens         off;
    }
    
    • 1
    • 2
    • 3

    禁止ip直接访问80端口

    server {
      listen                80 default;
      server_name           _;
      return                500;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    启动 web 服务 (vue 项目为例)

    server {
      # 项目启动端口
      listen            80;
      # 域名(localhost)
      server_name       _;
      # 禁止 iframe 嵌套
      add_header        X-Frame-Options SAMEORIGIN;
      
      # 访问地址 根路径配置
      location / {
        # 项目目录
        root 	    html;
        # 默认读取文件
        index           index.html;
        # 配置 history 模式的刷新空白
        try_files       $uri $uri/ /index.html;
      }
      
      # 后缀匹配,解决静态资源找不到问题
      location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ { 
        root           html/static/;
      }
      
      # 图片防盗链
      location ~/static/.*\.(jpg|jpeg|png|gif|webp)$ {
        root              html;
        valid_referers    *.deeruby.com;
        if ($invalid_referer) {
          return          403;
        }
      }
      
      # 访问限制
      location /static {
        root               html;
        # allow 允许
        allow              39.xxx.xxx.xxx;
        # deny  拒绝
        deny               all;
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    PC端和移动端使用不同的项目文件映射

    server {
      ......
      location / {
        root /home/static/pc;
        if ($http_user_agent ~* '(mobile|android|iphone|ipad|phone)') {
          root /home/static/mobile;
        }
        index index.html;
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    一个web服务,配置多个项目 (location 匹配路由区别)

    server {
      listen                80;
      server_name           _;
      
      # 主应用
      location / {
        root          html/main;
        index               index.html;
        try_files           $uri $uri/ /index.html;
      }
      
      # 子应用一
      location ^~ /store/ {
        proxy_pass          http://localhost:8001;
        proxy_redirect      off;
        proxy_set_header    Host $host;
        proxy_set_header    X-Real-IP $remote_addr;
        proxy_set_header    X-Forwarded-For
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
      }
      
      # 子应用二
      location ^~ /school/ {
        proxy_pass          http://localhost:8002;
        proxy_redirect      off;
        proxy_set_header    Host $host;
        proxy_set_header    X-Real-IP $remote_addr;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
      }
      
      # 静态资源读取不到问题处理
      rewrite ^/api/profile/(.*)$ /(替换成正确路径的文件的上一层目录)/$1 last;
    }
    
    # 子应用一服务
    server {
      listen                8001;
      server_name           _;
      location / {
        root          html/store;
        index               index.html;
        try_files           $uri $uri/ /index.html;
      }
      
      location ^~ /store/ {
        alias               html/store/;
        index               index.html index.htm;
        try_files           $uri /store/index.html;
      }
      
      # 接口代理
      location  /api {
        proxy_pass          http://localhost:8089;
      }
    }
    
    # 子应用二服务
    server {
      listen                8002;
      server_name           _;
      location / {
        root          html/school;
        index               index.html;
        try_files           $uri $uri/ /index.html;
      }
      
      location ^~ /school/ {
        alias               html/school/;
        index               index.html index.htm;
        try_files           $uri /school/index.html;
      }
      
      # 接口代理
      location  /api {
        proxy_pass          http://localhost:10010;
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77

    配置负载均衡

    upstream my_upstream {
      server                http://localhost:9001;
      server                http://localhost:9002;
      server                http://localhost:9003;
    }
    
    server {
      listen                9000;
      server_name           test.com;
    
      location / {
        proxy_pass          my_upstream;
        proxy_set_header    Host $proxy_host;
        proxy_set_header    X-Real-IP $remote_addr;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    SSL 配置 HTTPS

    server {
      listen                      80;
      server_name                 www.xxx.com;
      # 将 http 重定向转移到 https
      return 301 https://$server_name$request_uri;
    }
    
    server {
      listen                      443 ssl;
      server_name                 www.xxx.com;
      ssl_certificate             /etc/nginx/ssl/www.xxx.com.pem;
      ssl_certificate_key         /etc/nginx/ssl/www.xxx.com.key;
      ssl_session_timeout         10m;
      ssl_ciphers                 ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
      ssl_protocols               TLSv1 TLSv1.1 TLSv1.2;
      ssl_prefer_server_ciphers   on;
      
      location / {
        root                    /project/xxx;
        index                   index.html index.htm index.md;
        try_files               $uri $uri/ /index.html;
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    Java 异常
    LeetCode 0226. 翻转二叉树
    solr自定义定制自带core添加分词器,解决镜像没有权限问题
    【慢SQL性能优化】 一条SQL的生命周期
    艾默生Emerson EDI需求分析
    尚硅谷Flume(仅有基础)
    Nginx:vts模块(监控)
    C++征途 --- string容器
    STM32CUBEMX_创建时间片轮询架构的软件框架
    spring MVC源码探索之AbstractHandlerMethodMapping
  • 原文地址:https://blog.csdn.net/weixin_45788691/article/details/135129668