我有一个使用angularjs开发的应用程序,当访问dist /文件夹时加载整个应用程序.
尝试todo的是,当在angularjs上找不到页面时,尝试反向代理,我尝试进行以下设置但是nginx不允许在单个块中设置两次相同的位置
server {
listen 80;
server_name example.com;
keepalive_timeout 60;
client_max_body_size 10M;
root /var/lib/www/dist;
charset utf-8;
location / {
expires -1;
add_header Pragma "no-cache";
add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
root /var/lib/www/dist;
try_files $uri $uri/ /index.html =404;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_buffering off;
proxy_http_version 1.1;
proxy_set_header Connection "";
if (!-f $request_filename) {
proxy_pass http://app_root;
break;
}
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /var/lib/app/etc/templates;
}
}
Run Code Online (Sandbox Code Playgroud)
所以基本上,如果我想让它尝试并将其传递给proxy_pass http://app_root;任何人可以建议如何实现这个设置?
谢谢,
UPDATE
所以我正在尝试"Mohammad AbuShady"提出的方法,并将我的nginx设置更新为以下,但仍然无法正常工作,而是尝试在AngularJS应用中找到该页面,而不是转移到@proxy up_stream设置
upstream app_root {
server unix:/tmp/app_root.sock fail_timeout=0;
}
server {
listen 80;
server_name example.com;
keepalive_timeout 60;
client_max_body_size 10M;
root /var/lib/www/dist;
charset utf-8;
location / {
expires -1;
add_header Pragma "no-cache";
add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
root /var/lib/www/dist;
try_files $uri$args $uri$args/ $uri/ /index.html @proxy;
}
location @proxy {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_buffering off;
if (!-f $request_filename) {
proxy_pass http://app_root;
break;
}
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /var/lib/app/etc/templates;
}
}
Run Code Online (Sandbox Code Playgroud)
Moh*_*ady 13
你在想它,一个位置可以处理它,然后给它一个后备
location / {
# omitted extra settings
# check notes below
try_files $uri @proxy;
}
location @proxy {
# omitted proxy settings
proxy_pass http://app_root;
}
Run Code Online (Sandbox Code Playgroud)
笔记:
$uri/因为您index的服务器中没有.我也删除了/index.html,如果你想使用它,那么你可能想在服务器块中将其定义为索引并放$uri/回去
server {
index index.html;
location / {
try_files $uri $uri/ @proxy;
}
}
Run Code Online (Sandbox Code Playgroud)app_root,但我假设它是在其他地方定义的上游.| 归档时间: |
|
| 查看次数: |
6084 次 |
| 最近记录: |