nginx常用配置

pengjielee發表於2019-10-24

Nginx常用配置

1. 設定檔案最大上傳大小

http {
	client_max_body_size 200m;
}
複製程式碼

2. 設定gzip

http {
  gzip on;
  gzip_min_length 1000;
  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 image/svg+xml;
  gzip_vary on;
  gzip_disable "MSIE [1-6]\.";
}
複製程式碼

3. 設定反向代理

http {
    server {
        listen       80;
        server_name  admin.pengjielee.cn;
        index index.php index.html;
    
        location / {
          proxy_pass http://localhost:3003;
        }
    }
}
複製程式碼

4. 設定靜態資源目錄

http {
   server {
       location ~.*\.(js|css|html|gif|png|jpg|jpeg)$ {
          root /www/myserver/src/static;
          expires 1d;
        }
   }
}
複製程式碼

5. 設定跨域請求頭

http {
  server {
    listen       80;
    server_name  c.xx.com;
    index index.php index.html;

    location / {
      proxy_pass http://127.0.0.1:8082;
      proxy_set_header   Host c.xxx.com;
      proxy_set_header   X-Real-IP $remote_addr;
      proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
      add_header  Access-Control-Allow-Origin "http://topic.xxx.com";
      add_header  Access-Control-Allow-Credentials true;
      add_header  Access-Control-Allow-Methods "*";
      add_header  Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
    }

    location /tea/ {
      proxy_pass http://127.0.0.1:3000;
      proxy_set_header   Host c.xxx.com;
      proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header   X-Real-IP $remote_addr;
      proxy_set_header   X-Forwarded-Proto $scheme;
      proxy_set_header   Connection "";
    }
  }
}
複製程式碼

相關文章