以下目录层次结构的 NGINX 配置位置

问题描述 投票:0回答:1

我想知道以下位置是否适用于 NGINX 配置:

location ~ ^/(uk|us|de)/?(.*)? { 
    proxy_pass http://127.0.0.1:3000/$1/$2;  
    proxy_cache off;  
    proxy_buffering on;     
} 

预期的行为,因此当用户输入 us/test1 或 us/test2/help 或 uk/output/help 时 如果存在该目录层次结构,它会打开预期的页面吗?如果我在 NGINX conf 中应用它会起作用吗?如果没有 - 请建议我应该如何更改我的配置文件才能工作?

nginx nginx-config nginx-location
1个回答
0
投票

根据您的要求,您不需要在位置末尾添加 (.*),也不需要在代理 URL 中使用 $1 和 $2。以下是我用来代理前端和后端服务器请求的 nginx 配置,与您在 location (add|update|manage) 的要求相同

server {
    listen 443 ssl;
    http2 on;
    server_name dev.example.com;
    ssl_certificate /opt/homebrew/etc/nginx/ssl/development.com+8.pem;
    ssl_certificate_key /opt/homebrew/etc/nginx/ssl/development+8-key.pem;
    add_header 'Content-Security-Policy' 'upgrade-insecure-requests';

    # set max upload size
    client_max_body_size 512M;
    fastcgi_buffers 64 4K;

    # Enable gzip but do not remove ETag headers
    gzip on;
    gzip_vary on;
    gzip_comp_level 4;
    gzip_min_length 256;
    gzip_proxied expired no-cache no-store private no_last_modified no_etag auth;
    gzip_types application/atom+xml application/javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy;

    add_header Referrer-Policy "no-referrer" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-Download-Options "noopen" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Permitted-Cross-Domain-Policies "none" always;
    add_header X-Robots-Tag "none" always;
    add_header X-XSS-Protection "1; mode=block" always;

    # Matches 
    # /add
    # /update
    # /manage
    # /add/user
    # /update/user
    # /manage/user
    location ~ ^/(add|update|manage) {
        add_header Access-Control-Allow-Origin *;
        add_header Content-Type application/gzip;
        include /opt/homebrew/etc/nginx/conf.d/proxy.conf;
        proxy_pass http://127.0.0.1:8080;
    }

    location ~ \.css {
        add_header Access-Control-Allow-Origin *;
        add_header Content-Type text/css;
        include /opt/homebrew/etc/nginx/conf.d/proxy.conf;
        proxy_pass http://127.0.0.1:3000;
    }

    location ~ \.js {
        add_header Access-Control-Allow-Origin *;
        add_header Content-Type application/x-javascript;
        include /opt/homebrew/etc/nginx/conf.d/proxy.conf;
        proxy_pass http://127.0.0.1:3000;
    }

    location / {
        include /opt/homebrew/etc/nginx/conf.d/proxy.conf;
        proxy_pass http://127.0.0.1:3000;
    }

    include /opt/homebrew/etc/nginx/conf.d/security.conf;
}

/opt/homebrew/etc/nginx/conf.d/proxy.conf 的内容

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_set_header X-Forwarded-Proto $scheme;

/opt/homebrew/etc/nginx/conf.d/security.conf的内容

location ~ /\.ht {
    deny  all;
}

要测试位置匹配是否正常工作,您可以使用以下链接 https://nginx.viraptor.info/

注意: 请记住在末尾添加 /,因为只有当我们使用的其他正则表达式(例如:~ ^/(add|update|manage))与您拥有的 URL 不匹配时才应匹配它已请求

© www.soinside.com 2019 - 2024. All rights reserved.