0
我們定義了兩個服務器對象,一個用於example.com另一個用於www.example.com。如果用戶在移動瀏覽器上,我們希望自動將用戶重定向到移動網站www.example.com/m。配置NGINX用於web和移動站點重定向
在tomcat上部署的應用程序的移動和Web版本有兩個不同的項目,並且API僅通過Web版本公開。移動項目使用相同的Web API,因此根目錄很常見。
配置爲:
server {
#listen 80 is default
listen 80;
listen 443 ssl;
ssl on;
ssl_certificate /path/to/certificates/www.example.com.chained.crt;
ssl_certificate_key /path/to/certificates/example.com.key;
location/{
if ($http_user_agent ~* "(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm(os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino") {
return 301 https://www.example.com/m;
}
return 301 https://www.example.com;
}
location /m {
}
server_name example.com;
}
server {
listen 80;
listen 443 ssl;
ssl on;
ssl_certificate /path/to/certificates/www.example.com.chained.crt;
ssl_certificate_key /path/to/certificates/example.com.key;
server_name www.example.com;
if ($scheme = http) {
return 301 https://$server_name$request_uri;
}
root /usr/share/tomcat/webapps/ExampleApp;
location/{
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://localhost:8080/;
}
}
此配置正常工作時:
- example.com從移動
- example.com或www.example.com進入從網絡
但是,它不起作用時:
- www.example.com從手機進入(它會打開網頁而不是手機網站)。
當用戶從手機進入www.example.com時,我們如何重定向到www.example.com/m?
感謝@Richard,根據您的建議實施,移動客戶端無法進行API調用,因爲所有的API調用都被重寫爲/ m例如www.example.com/login被重定向到www.example.com/m/login。這些API在/ m處不可用,但在/處。我們如何解決這個問題?我嘗試了正則表達式的實現,但它不適合我。 –
@MohammadAslam我不確定你想要重寫哪個URI。如果你只是想重寫'/'到'/ m',那麼使用'rewrite ^/$/m/last;'。因爲沒有重定向循環,所以在這種情況下你可能不需要'location/m'。 –
感謝所做的更改,工作得很好 –