简体   繁体   English

我如何从 nginx 的多个位置提供相同的位置?

[英]how can i served same location from many location in nginx?

my nginx version: 1.17.3我的 nginx 版本:1.17.3

I want served same template from many url my site in nginx我想从 url 我的网站在 nginx 中提供相同的模板

for example-例如-

localhost:0000/adprofit
localhost:0000/adprofit/0
localhost:0000/adprofit/0/2
localhost:0000/adprofit/asdlk/asd/asd
localhost:0000/adprofit/sand/12312/asd/?AS?Dsa

in any case if i catch same keyword like 'adprofit'.在任何情况下,如果我捕捉到相同的关键字,如“adprofit”。 my site user must be served 'adprofit.html' template.我的网站用户必须使用“adprofit.html”模板。 BUT, NO REWRITE, NO REDIRECT.但是,没有重写,没有重定向。 i want keep user request url.我想保留用户请求 url。

My directory:我的目录:

build
|
|-index.html
|-index.js
|-adprofit
|   |
|   |-adprofit.js
|   |-adprofit.html
|
|-another directories...

I succeed served from diffrent url used 'alias'.我成功地从不同的 url 使用“别名”服务。

location /all {
     allow all;
     alias ./build/adprofit;
     index adprofit.html;
}

in this case keep url localhost:0000/all/ and view template adprofit.html .在这种情况下,请保留 url localhost:0000/all/并查看模板adprofit.html

and this case too.这个案子也是。

Code:代码:

location /adprofit/all {
     allow all;
     alias ./build/adprofit;
     index adprofit.html;
}
url : 'localhost:0000/adprofit/all/'

my nginx code:我的 nginx 代码:

server {
     listen       8888;
     server_name  192.168.0.25;
     add_header 'Access-Control-Allow-Origin' '*';
     access_log  logs/host.access.log  main;

     location / {
      allow all;
      root ./build;
      index  index.html;
      }

 location /adprofit {
  allow all;
  alias ./build/adprofit;
  index adprofit.html;
 }
}

my try case:我的尝试案例:

location /adprofit/ {
     allow all;
     alias ./build/adprofit;
     index adprofit.html;
}
succeed url : 'localhost:0000/adprofit/'
failed url : 'localhost:0000/adprofit/[anykeyword]' => 404 error page

code2)
location ~*/adprofit/(all|update|event) {
     allow all;
     alias ./build/adprofit;
     index adprofit.html;
}
succeed url : 'localhost:0000/adprofit/'
failed url : 'localhost:0000/adprofit/all and update and event' => 403 Forbidden

how can i resolve this situation?我该如何解决这种情况?

try_files directive is a better approach here. try_files指令在这里是一种更好的方法。 This config will always point to the same files (adprofit.html).此配置将始终指向相同的文件 (adprofit.html)。

Tested and works:测试和工作:

/adprofit

/adprotif/random

location /adprofit {
    alias /tmp/;
    try_files /adprofit.html =404
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM