112 lines
2.9 KiB
Nginx Configuration File
112 lines
2.9 KiB
Nginx Configuration File
|
|
# Nginx 配置文件 - 天远查 SPA + SEO 静态页面
|
||
|
|
# 自动生成时间: 2026/2/25 11:04:50
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
server {
|
||
|
|
listen 80;
|
||
|
|
server_name www.tianyuancha.cn;
|
||
|
|
charset utf-8;
|
||
|
|
|
||
|
|
|
||
|
|
# 日志配置
|
||
|
|
access_log /var/log/nginx/www.tianyuancha.cn-access.log;
|
||
|
|
error_log /var/log/nginx/www.tianyuancha.cn-error.log;
|
||
|
|
|
||
|
|
# 爬虫检测
|
||
|
|
set $is_bot 0;
|
||
|
|
|
||
|
|
# Google
|
||
|
|
if ($http_user_agent ~* (googlebot|googlebot-image|googlebot-news|mediapartners-google|adsbot-google)) {
|
||
|
|
set $is_bot 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
# 百度
|
||
|
|
if ($http_user_agent ~* (baiduspider|baiduspider-mobile)) {
|
||
|
|
set $is_bot 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
# 必应
|
||
|
|
if ($http_user_agent ~* (bingbot|msnbot)) {
|
||
|
|
set $is_bot 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
# 360
|
||
|
|
if ($http_user_agent ~* "360spider") {
|
||
|
|
set $is_bot 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
# 搜狗
|
||
|
|
if ($http_user_agent ~* "(sogou spider|sogou-orion)") {
|
||
|
|
set $is_bot 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
# 其他爬虫
|
||
|
|
if ($http_user_agent ~* "(spider|crawl|bot|slurp|yandex|duckduckbot|bytespider|yisouspider)") {
|
||
|
|
set $is_bot 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
# 根路径处理
|
||
|
|
location = / {
|
||
|
|
if ($is_bot = 1) {
|
||
|
|
root /var/www/tyc-webview-v2/static-pages;
|
||
|
|
try_files /index.html /index.html;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
root /var/www/tyc-webview-v2/dist;
|
||
|
|
try_files $uri $uri/ /index.html;
|
||
|
|
}
|
||
|
|
|
||
|
|
# 静态页面处理
|
||
|
|
location ~ ^/([^/]+)(/.*)?$ {
|
||
|
|
# 将路径转换为文件名
|
||
|
|
set $static_filename $1;
|
||
|
|
set $rest_path $2;
|
||
|
|
|
||
|
|
if ($is_bot = 1) {
|
||
|
|
root /var/www/tyc-webview-v2/static-pages;
|
||
|
|
try_files /$static_filename.html /index.html;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
root /var/www/tyc-webview-v2/dist;
|
||
|
|
try_files $uri $uri/ /index.html;
|
||
|
|
}
|
||
|
|
|
||
|
|
# SPA 路由回退
|
||
|
|
location / {
|
||
|
|
root /var/www/tyc-webview-v2/dist;
|
||
|
|
try_files $uri $uri/ /index.html;
|
||
|
|
add_header Cache-Control "no-cache";
|
||
|
|
}
|
||
|
|
|
||
|
|
# 静态资源缓存
|
||
|
|
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
|
||
|
|
root /var/www/tyc-webview-v2/dist;
|
||
|
|
expires 1y;
|
||
|
|
add_header Cache-Control "public, immutable";
|
||
|
|
}
|
||
|
|
|
||
|
|
# API 代理
|
||
|
|
location /api/ {
|
||
|
|
proxy_pass http://localhost:8080;
|
||
|
|
proxy_http_version 1.1;
|
||
|
|
proxy_set_header Upgrade $http_upgrade;
|
||
|
|
proxy_set_header Connection 'upgrade';
|
||
|
|
proxy_set_header Host $host;
|
||
|
|
proxy_set_header X-Real-IP $remote_addr;
|
||
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||
|
|
proxy_cache_bypass $http_upgrade;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Gzip 压缩
|
||
|
|
gzip on;
|
||
|
|
gzip_vary on;
|
||
|
|
gzip_min_length 1024;
|
||
|
|
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml+rss application/javascript application/json;
|
||
|
|
|
||
|
|
# 安全头
|
||
|
|
add_header X-Frame-Options "SAMEORIGIN";
|
||
|
|
add_header X-Content-Type-Options "nosniff";
|
||
|
|
add_header X-XSS-Protection "1; mode=block";
|
||
|
|
}
|