Nginx 常用配置

Nginx 常用配置

下載站點

1
2
3
4
5
location /download {    
alias /home/user/elivolution; # 資料夾路徑
autoindex on;
charset utf-8;
}
  • alias 資料夾路徑
  • autoindex 開啟目錄瀏覽
  • charset 防止中文亂碼需加入 utf-8 or gbk

反向代理並啟用 CORS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server {
#以上審略

location ~ ^/(computer/|mobile|home) {
add_header 'Access-Control-Allow-Origin' *網域 server_name*;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';

if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}

proxy_redirect off;
proxy_set_header host $host;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-forward-for $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:3000;
}
}

http 轉 https

1
2
3
4
5
6
server {
listen 80;
listen [::]:80;
server_name server_name;
return 301 https://$host$request_uri;
}

Vue-router

1
2
3
4
5
6
7
8
server {
#以上審略

location / {
root /home/deploy/project_name/dist;
try_files $uri $uri/ /index.html;
}
}

防止機器人

1
2
3
4
5
6
7
server {
#以上審略

if ($http_user_agent ~* "SemrushBot|PetalBot|BOT/0.1|YandexBot|Linguee Bot" ) {
return 403;
}
}

Thank you! 😄


參考