nginx - Nginx - 从多个子目录索引静态文件夹,为 GeoIp 重写
问题描述
我正在构建一个静态站点,用户将在其中看到基于其国家/地区的不同 index.html 文件。
我的计划是在 nginx 中使用 GeoIp 模块,如果来自国家 1 的人访问我的域www.example.com,他的 URL 将被重写为www.example.com/country1并且其静态站点数据将提供给 from 文件夹 / var/www/site/country1/index.html 。类似的逻辑将应用于国家 2 和国家 3。如果此人不属于这 3 个国家/地区,他将看到位于 /var/www/site/index.html 的默认页面
但问题是我无法找到解决方案。有时我看到 404 错误,有时它显示 500 错误指示多个内部重定向,有时它只显示 /var/www/site/country1/index.html 文件而没有任何样式,即无法访问位于 /var/ 中的任何资产文件www/site/country1/assets。
这是我的 nginx.conf 的样子,
user www-data;
pid /run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 65535;
error_log /var/log/nginx/error.log;
include /usr/share/nginx/modules/*.conf;
load_module modules/ngx_http_geoip2_module.so;
load_module modules/ngx_pagespeed.so;
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;
events {
multi_accept on;
worker_connections 65535;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$http_x-webp-convert-log" ';
charset utf-8;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
log_not_found off;
types_hash_max_size 2048;
client_max_body_size 16M;
include mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log warn;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
server {
listen 80 ;
server_name _;
deny all;
return 444;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
这是我的网站配置文件,
geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
$geoip2_data_country_iso_code country iso_code;
}
map $geoip2_data_country_iso_code $redirect_country {
default other;
GB country1 ;
US country2 ;
IN country3 ;
}
server {
listen 80;
server_name www.example.com;
index index.html;
root /var/www/static/www.example.com;
client_max_body_size 50M;
include /etc/nginx/pagespeed.conf;
include /etc/nginx/conf.d/security.conf ;
if ($redirect_country = country1)
{
#This is where i am making the changes
#rewrite "^.*$" /country1 break ;
#rewrite ^ https://www.example.com/country1$uri break;
#rewrite ^ https://www.example.com/country1 break ;
#return 444;
#rewrite ^ $scheme://$server_name/country1$request_uri break;
#rewrite ^/(.*)$ $scheme://www.example.com$uri break;
#return 301 https://www.example.com/country1/index.html break ;
#rewrite ^ https://www.example.com/(.*) https://www.example.com/country1 $1 break;
#rewrite ^/(.*)$ https://www.example.com/country1/$1 redirect;
# rewrite "^.*$" /country1/country1.html$uri break;
rewrite "^.*$" /country1$request_uri break;
}
if ($redirect_country = country3)
{
rewrite "^.*$" /country3$uri break ;
}
if ($redirect_country = country2)
{
rewrite "^.*$" /country2$uri break ;
}
if ($redirect_country = other)
{
rewrite "^.*$" $request_uri break ;
}
access_log /var/log/nginx/sg.foodlisa.tk.access.log;
error_log /var/log/nginx/sg.foodlisa.tk.error.log;
location /
{
#root /var/www/static/www.example.com;
# try_files $uri /index.html;
#try_files $uri = return 404;
try_files $uri $uri/ /index.html =404;
}
location /country1
{
#This is where i am making the changes
index country1.html ;
root /var/www/static/www.example.com/country1/;
#try_files $uri /country1/index.html;
#try_files $uri /index.html;
#break;
autoindex off;
# try_files $uri $uri/ /var/www/static/www.example.com/country1/index.html?q=$uri&$args;
try_files $uri $uri/ /index.html =404;
}
location /country2
{
root /var/www/static/www.example.com/country2;
index index.html ;
}
location /country3
{
root /var/www/static/www.example.com/country3;
index index.html ;
try_files $uri /index.html;
}
location /sw.js
{
add_header Cache-Control "no-cache";
proxy_cache_bypass $http_pragma;
proxy_cache_revalidate on;
expires off;
access_log off;
}
location ~ \.css
{
add_header Content-Type text/css;
}
location ~ \.js
{
add_header Content-Type application/x-javascript;
}
}
解决方案
推荐阅读
- postgresql - Postgres自动更改dblink的密码
- hyperledger-fabric - Hyperledger Fabric 中使用了什么样的共识?
- c++ - WM_LBUTTONDOWN windows消息和DragDetect的简要说明
- java - Java中没有BigInteger的Karatsuba算法,递归时出现意外行为
- javascript - 通过 oninput 函数获取可编辑标签的值?
- github - 表中的转义管道(|)字符。在 Github 页面中
- java - Wicket Ajax 测试将 DropDownChoice 重置为 null
- database - Keyclock 数据库与 Spring Boot 数据库
- api - Asp.net core 2.2 api路由
- neural-network - 历代持续损失