惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
Cloudbric
Cloudbric
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tenable Blog
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
P
Proofpoint News Feed
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
L
LangChain Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东

博客园 - NewSea

Linux 安装IntelAx211无线网卡 解决 K8sApi 部署后报 Unknown apiVersionKind apps/v1/Deployment is it registered? Docker 启动前后端脚本 docker 一键启动 mariadb 分区脚本 Spring笔记--@ConditionalOnBean坑 K8s笔记 ubuntu18+k8s单机版+kuboard+harbor安装笔记 yapi 自定义Json的数据类型 k3s+rancher+harbor 笔记 postgresql 笔记 vscode 笔记 发布Jar包到中央仓库 Java开发笔记汇总 - NewSea - 博客园 css 适配 nui-app 笔记 Java里的不能与无用. SSH 配置 Swagger 配置
Nginx笔记
NewSea · 2020-06-08 · via 博客园 - NewSea

官网:

http://nginx.org/
http://nginx.org/en/docs/

文档:

文档可以参考淘宝的:

http://tengine.taobao.org

安装

安装(CentOs7):

/etc/yum.repos.d/nginx.repo

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

yum install nginx

安装(Ubuntu16.04)

sudo apt install nginx

离线安装

离线安装gcc,有一个比较省事的办法, 从 CentOs7 的镜像里,找 package 文件夹, 里有有很多软件安装包。 把相关安装包 上传到服务器, 安装即可。

下载 pcre :https://sourceforge.net/projects/pcre/files/pcre/

下载 zlib : http://www.zlib.net/

下载 nginx: http://nginx.org/en/download.html

命令:

启动: nginx
停止:nginx -s stop
退出: nginx -s quit
重新加载配置文件: nginx -s reload

配置文件:

查询安装软件相关文件的位置: rpm -qc nginx

一般的,配置文件可能会在:

/etc/nginx
/usr/local/nginx/conf
/usr/local/etc/nginx

查看端口占用

http://blog.csdn.net/hsd2012/article/details/51384907

lsof -i: 80
netstat -anp | grep 80

常用配置

vue 单页面应用配置

  location /{
    try_files $uri $uri/ /index.html =404;
  }

ubuntu16.04配置多个站点

cd /etc/nginx

cp sites-available/default sites-available/2080

修改 2080文件的端口和地址。

sudo ln -s sites-available/2080 sites-enabled/

sudo nginx -s reload

Nginx端口转发:

server {
      listen 80;
      server_name localhost;
      location / {
            proxy_pass http://x.x.x.x:9500;
            proxy_set_header Host $host:80;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Via "nginx";
      }
}

二级目录转发

location /test/ {
                proxy_pass http://127.0.0.1:2081/;

                proxy_set_header  Host  $host;
                proxy_set_header  X-Real-IP  $remote_addr;
                proxy_set_header  REMOTE-HOST  $remote_addr;
                proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;

        }

要点: location 前后要有 / , proxy_pass 前要有 http,后要有 / .

HTTP转发

upstream git {
        server 172.17.0.3:80;
}
server{
        listen 80;
        server_name git.huidd365.com;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404
                proxy_pass http://git;
        }
}

虚拟目录

location /school  {
	alias /opt/edu/corp-web/;

	try_files $uri $uri/ /index.html?$args;
}

location /school-api/ {
	proxy_pass http://127.0.0.1:9113/;
}

注:
1. location 后面不要写 /
2. try_files $uri $uri/ /index.html?$args; 定义了重试的顺序: 文件 > 目录 > /index.html
3. alias 定义了虚拟目录

Nginx 上传大小限制

413 Request Entity Too Large

增加如下两行到nginx.conf的http{}段, 增大nginx上传文件大小限制

      # 设置允许发布内容为8M
      client_max_body_size 8M;
      client_body_buffer_size 128k;

      proxy_connect_timeout 1200s;
      proxy_send_timeout 1200s; 
      proxy_read_timeout 1200s;  

相关的 spring boot 设置

Spring Boot 配置修改为: 2.0 之后单位Mb改为MB了

--spring.servlet.multipart.max-file-size=2000MB
--spring.servlet.multipart.max-request-size=2000MB
--server.tomcat.max-http-post-size:20000MB

openssl创建Https证书

http://blog.csdn.net/typ2004/article/details/52779663

sudo openssl genrsa -out server.key 1024
sudo openssl req -new -key server.key -out server.csr
sudo openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

去除 第一条命令的 -des3 就不会有密码了.

server {  
     listen 443 ssl default_server;
     listen [::]:443 ssl default_server;

     ###其他配置

     ssl                  on;  
     ssl_certificate      /var/www/ssl/server.crt;  
     ssl_certificate_key  /var/www/ssl/server_nopwd.key; 
     ### 
 } 

宕机自动切换

http://blog.sina.com.cn/s/blog_4af978980102wekn.html

按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器 , upstream hash $request_uri

配置示例

配置示例
#使用的用户和组,window下不指定 
#user nobody; 
#指定工作衍生进程数(一般等于CPU总和数或总和数的两倍,例如两个四核CPU,则总和数为8) 
worker_processes 1; 
#指定错误日志文件存放路径,错误日志级别可选项为【debug|info|notice|warn|error|crit】 
#error_log logs/error.log; 
#error_log logs/error.log notice; 
error_log logs/error.log info; 
#指定pid存放路径 
#pid logs/nginx.pid; 

#工作模式及连接数上限 
events { 
    #使用网络I/O模型,Linux系统推荐使用epoll模型,FreeBSD系统推荐使用kqueue;window下不指定 
    #use epoll; 
    #允许的连接数 
    worker_connections 1024; 
} 

#设定http服务器,利用他的反向代理功能提供负载均衡支持 
http { 
    #设定mime类型 
    include 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 logs/access.log main; 
    log_format main '$remote_addr - $remote_user [$time_local]' 
    '"$request" $status $bytes_sent' 
    '"$http_referer" "$http_user_agent" "$http_x_forwarded_for"' 
    '"$gzip_ratio"'; 
    log_format download '$remote_addr - $remote_user [$time_local]' 
    '"$request" $status $bytes_sent' 
    '"$http_referer" "$http_user_agent"' 
    '"$http_range" "$sent_http_content_range"'; 

    #设定请求缓冲 
    client_header_buffer_size 1k; 
    large_client_header_buffers 4 4k; 

    #设定access log 
    access_log logs/access.log main; 
    client_header_timeout 3m; 
    client_body_timeout 3m; 
    send_timeout 3m; 

    sendfile on; 
    tcp_nopush on; 
    tcp_nodelay on; 
    #keepalive_timeout 0; 
    keepalive_timeout 65; 

    #开启gzip模块 
    gzip on; 
    gzip_min_length 1100; 
    gzip_buffers 4 8k; 
    gzip_types text/plain application/x-javascript text/css application/xml; 

    output_buffers 1 32k; 
    postpone_output 1460; 

    server_names_hash_bucket_size 128; 
    client_max_body_size 80m;  #最大上传的文件大小。

    fastcgi_connect_timeout 300; 
    fastcgi_send_timeout 300; 
    fastcgi_read_timeout 300; 
    fastcgi_buffer_size 64k; 
    fastcgi_buffers 4 64k; 
    fastcgi_busy_buffers_size 128k; 
    fastcgi_temp_file_write_size 128k; 
    gzip_http_version 1.1; 
    gzip_comp_level 2; 
    gzip_vary on; 

    #设定负载均衡的服务器列表 
    upstream localhost { 
        #根据ip计算将请求分配各那个后端tomcat,可以解决session问题 
        ip_hash; 
        #同一机器在多网情况下,路由切换,ip可能不同 
        #weigth参数表示权值,权值越高被分配到的几率越大 
        #server localhost:8080 weight=1; 
        #server localhost:9080 weight=1; 
        server 192.168.0.4:801 max_fails=2 fail_timeout=600s; 
        server 192.168.0.4:802 max_fails=2 fail_timeout=600s; 
    } 

    #设定虚拟主机 
    server { 
        listen 800; 
        server_name 192.168.0.4; 

        #charset koi8-r; 
        charset UTF-8; 
        #设定本虚拟主机的访问日志 
        access_log logs/host.access.log main; 
        #假如访问 /img/*, /js/*, /css/* 资源,则直接取本地文档,不通过squid 
        #假如这些文档较多,不推荐这种方式,因为通过squid的缓存效果更好 
        #location ~ ^/(img|js|css)/ { 
            # root /data3/Html; 
            # expires 24h; 
        # } 
        #对 "/" 启用负载均衡 
        location / { 
            root html; 
            index index.html index.htm index.aspx; 

            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; 
            #允许客户端请求的最大单个文件字节数 
            client_max_body_size 10m; 
            #缓冲区代理缓冲用户端请求的最大字节数,可以理解为先保存到本地再传给用户 
            client_body_buffer_size 128k; 
            #跟后端服务器连接超时时间 发起握手等候响应超时时间 
            proxy_connect_timeout 12; 
            #连接成功后 等待后端服务器响应时间 其实已进入后端的排队之中等候处理 
            proxy_read_timeout 90; 
            #后端服务器数据回传时间 就是在规定时间内后端服务器必须传完所有数据 
            proxy_send_timeout 90; 
            #代理请求缓存区 这个缓存区间会保存用户的头信息一共Nginx进行规则处理 一般只要能保存下头信息即可 
            proxy_buffer_size 4k; 
            #同上 告诉Nginx保存单个用的几个Buffer最大用多大空间 
            proxy_buffers 4 32k; 
            #如果系统很忙的时候可以申请国内各大的proxy_buffers 官方推荐 *2 
            proxy_busy_buffers_size 64k; 
            #proxy 缓存临时文件的大小 
            proxy_temp_file_write_size 64k; 
            proxy_next_upstream error timeout invalid_header http_500 http_503 http_404; 
            proxy_max_temp_file_size 128m; 

            proxy_pass http://localhost; 
        } 

        #error_page 404 /404.html; 

        # redirect server error pages to the static page /50x.html 
        # 
        error_page 500 502 503 504 /50x.html; 
        location = /50x.html { 
            root 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; 
        #} 
    } 


    # another virtual host using mix of IP-, name-, and port-based configuration 
    # 
    #server { 
        # listen 8000; 
        # listen somename:8080; 
        # server_name somename alias another.alias; 

        # location / { 
            # root html; 
            # index index.html index.htm; 
        # } 
    #} 


    # HTTPS server 
    # 
    #server { 
        # listen 443; 
        # server_name localhost; 

        # ssl on; 
        # ssl_certificate cert.pem; 
        # ssl_certificate_key cert.key; 

        # ssl_session_timeout 5m; 

        # ssl_protocols SSLv2 SSLv3 TLSv1; 
        # ssl_ciphers HIGH:!aNULL:!MD5; 
        # ssl_prefer_server_ciphers on; 

        # location / { 
            # root html; 
            # index index.html index.htm; 
        # } 
    #} 
}

这个文件配置比较全了。重点说几个:

  1. client_max_body_size , 表示最大上传的文件大小, 有时我们做了转发,后台程序设置的最大上传文件大小为100mb , 但是发现不能上传大于2MB的文件,原因就是经过 nginx 时,nginx 也做了限制。

  2. proxy_connect_timeout ,proxy_send_timeout, proxy_read_timeout 表示连接后端时间,发送到前端时间,读取后端流的时间。做转发时,也应该设置大一些。