2017-03-24 36 views
1

我成功地讓nginx把所有東西轉發給了tomcat。現在我想在'myserver.com'上運行靜態html頁面,並且在app.myserver.com下運行我的tomcat應用程序。nginx轉發到tomcat如果文件不存在

由於互聯網上已經有鏈接,不指向app.myserver.com我需要以某種方式處理。

基本上這個想法是讓nginx檢查請求的靜態html文件是否存在。 如果它不存在,它應該將它轉發給tomcat(即@app)。這是我目前的配置。

location/{ 
    root /usr/share/nginx/html; 
    try_files/$uri @app; 
} 


location @app { 

# 
# Forward SSL so that Tomcat knows what to do 
proxy_set_header X-Forwarded-Host $host; 
proxy_set_header X-Forwarded-Server $host; 
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 

proxy_pass http://tomcat1/; 
proxy_set_header X-Forwarded-Proto https; 
#proxy_redirect off; 
proxy_connect_timeout  240; 
proxy_send_timeout   240; 
proxy_read_timeout   240; 
} 

我收到以下錯誤

2017/03/24 20:49:12 [emerg] 7639#0: "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /etc/nginx/sites-enabled/myserver.com:134 

什麼是我誤會?

回答

0

proxy_pass語句中刪除URI。用途:

proxy_pass http://tomcat1; 

查看this document瞭解詳情。

此外,try_files聲明中不應包含/。用途:

try_files $uri @app;