关于前端:Nginx-运维问题调研-有问题就更新

门路开端不加斜杠导致的重定向

参考网址: CSDN

当拜访如下地址时:

https://sdri.jtsj.com.cn:1806… nginx会尝试在开端增加斜杠. 其实就是开启一个301的重定向, 然而当nginx监听的端口和对外的端口不统一的时候会导致出错。

nginx加完斜杠后,拜访地址变成了如下:

https://sdri.jtsj.com.cn:8069…

8069端口是内网本地端口,而18069才是公网端口,所以导致网页加载失败。

解决办法:

新版本nginx(≥1.11.8)能够通过设置 absolute_redirect off; 来解决

server {
    listen       8080;
    server_name  www.mydomain.com;
     
    absolute_redirect off;    #勾销绝对路径的重定向
     
    root   html;
    ...
}

nginx代理门路问题

参考资料: 简书

简略的说,不带URI的形式只替换主机名,带URI的形式替换整个URL。

理论例子:

server {
   listen       80;
   server_name  localhost;

   location /api1/ {
           proxy_pass http://localhost:8080;
        }
   # http://localhost/api1/xxx -> http://localhost:8080/api1/xxx


   location /api2/ {
           proxy_pass http://localhost:8080/;
        }
   # http://localhost/api2/xxx -> http://localhost:8080/xxx


   location /api3 {
           proxy_pass http://localhost:8080;
        }
   # http://localhost/api3/xxx -> http://localhost:8080/api3/xxx


   location /api4 {
           proxy_pass http://localhost:8080/;
        }
   # http://localhost/api4/xxx -> http://localhost:8080//xxx,请留神这里的双斜线,好好剖析一下。


   location /api5/ {
           proxy_pass http://localhost:8080/haha;
        }
   # http://localhost/api5/xxx -> http://localhost:8080/hahaxxx,请留神这里的haha和xxx之间没有斜杠,剖析一下起因。

   location /api6/ {
           proxy_pass http://localhost:8080/haha/;
        }
   # http://localhost/api6/xxx -> http://localhost:8080/haha/xxx

   location /api7 {
           proxy_pass http://localhost:8080/haha;
        }
   # http://localhost/api7/xxx -> http://localhost:8080/haha/xxx

   location /api8 {
           proxy_pass http://localhost:8080/haha/;
        }
  # http://localhost/api8/xxx -> http://localhost:8080/haha//xxx,请留神这里的双斜杠。
}

vue等history路由配置

try_files $uri $uri/ $uri/index.html;

nginx限度文件上传配置

client_max_body_size 1024M; 上传文件大小限度

sendfile on; 设置为on示意启动高效传输文件的模式

keepalive_timeout 1800;放弃连贯的工夫,默认65s

gzip压缩和cache缓存配置

http {
     ##缓存cache参数配置##  
    proxy_connect_timeout 5;  
    proxy_read_timeout 60;  
    proxy_send_timeout 5;  
    proxy_buffer_size 16k;  
    proxy_buffers 4 64k;  
    proxy_busy_buffers_size 128k;  
    proxy_temp_file_write_size 128k;  
    #缓存到nginx的本地目录  
    proxy_temp_path  C:/Users/chendm/Downloads/nginx-1.18.0/temp;  
    proxy_cache_path C:/Users/chendm/Downloads/nginx-1.18.0/temp/cache_temp levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g; 

    gzip  on;           #关上gzip压缩性能  
    gzip_min_length 1k; #压缩阈值  
    gzip_buffers 4 16k; #buffer 不必批改  
    gzip_comp_level 2;  #压缩级别:1-10,数字越大压缩的越好,工夫也越长  
    gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;  #        压缩文件类型  
    gzip_vary off;      #跟Squid等缓存服务无关,on的话会在Header里减少 "Vary: Accept-Encoding"  
    gzip_disable "MSIE [1-6]\.";  #IE1-6版本不反对gzip压缩   
    
    server {
        # 是必须的, 否则缓存的location找不到动态资源门路
        root C:/publish-server;
        location / {
            root   C:/publish-server;
            index  index.html index.htm;
            autoindex on;
            # cache_one 名称是后面http局部keys_zone定的
            proxy_cache cache_one;
            proxy_cache_valid 168h;
            # 疏忽浏览器的头信息( 除非浏览器开F12加no-cache )
            proxy_ignore_headers Set-Cookie Cache-Control;
            proxy_hide_header Cache-Control;
            proxy_hide_header Set-Cookie;
            # 文件上传限度的大小
            client_max_body_size 2000m;
        }
        
        # 后面必须有root否则,nginx找不到资源门路会404
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
            expires  15d;
        }
        location ~ .*\.(js|css)?$ {
            expires  1d;
        }
    }
}

拜访限度

#限度用户连接数来预防DOS攻打
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;
#限度同一客户端ip最大并发连接数
limit_conn perip 2;
#限度同一server最大并发连接数
limit_conn perserver 20;
#限度下载速度,依据本身服务器带宽配置
limit_rate 300k; 

高效数据传输配置

#开启文件的高效传输模式。tcp_nopush和tcp_nodelay可防止网络及磁盘i/o阻塞,晋升nginx工作效率;
sendfile on;
#数据包不会马上传送进来,等到数据包最大时,一次性的传输进来,这样有助于解决网络梗塞。
tcp_nopush on;
#只有有数据包产生,不论大小多少,就尽快传输
tcp_nodelay on;

负载平衡配置

http {
    # 定义集群
    upstream demo {
        server localhost:1111;
        server localhost:1112;
        server localhost:1113;
        server localhost:1114;
        server localhost:1115;
    }
    
    server {
        location / {
            proxy_pass http://demo
        }
    }
}

配置 申请的Referer

    map $http_referer $ref {
        default $http_referer;
        ~(http:\/\/hello)(.*)  $1abc$2;
        ~http://why   http://hello;
        }

在须要的location块中增加

proxy_set_header referer $ref;

成果

原referer = http://why的referer 就会改成 http://hello
原referer = http://hello/world 会改成http://helloabc/world

Nginx启动失败 invalid PID number

指定nginx配置文件即可

nginx -c /etc/nginx/nginx.conf

linux 查看过程pid

ps -ef|grep + 名称

linux 杀死过程

kill -9 + pid

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理