relative path resolution problem.
Added by Emir Cem over 13 years ago
Hello,
I am in trouble with relative path resolution. When i put css and javascript files into Css and Js directories, wt cannot resolve relative path. I used nginx for proxy pass and here is my code.
*application.cpp
#include "application.h"
#include
#include
using namespace Wt;
Application::Application(const WEnvironment &rEnvironment)
: WApplication(rEnvironment)
{
std::cout << "RESOLVE_RELATIVE_URL=" << resolveRelativeUrl("test.css") << std::endl;
std::cout << "RESOLVE_RELATIVE_URL=" << resolveRelativeUrl("Css/test.css") << std::endl;
useStyleSheet("test.css");
require("test.js");
// useStyleSheet("Css/test.css");
// require("Js/test.js");
doJavaScript("test();");
root()->addWidget(new WLabel("Test"));
}
First try:
- nginx conf:
location / {
proxy_pass http://localhost:8080;
proxy_redirect default;
}
<!-- -->
- cmd line: ./path_test ---docroot=. ---http-address=127.0.0.1 ---http-port=8080
RESULT= working good.
Second try:
- nginx conf:
location /test {
proxy_pass http://localhost:8080;
proxy_redirect default;
}
<!-- -->
- cmd line: ./path_test ---docroot=. ---http-address=127.0.0.1 ---http-port=8080
RESULT= not working (404 not found) Application output= "GET /test HTTP/1.0" 404 85
Third try:
- nginx conf:
location /test {
proxy_pass http://localhost:8080;
proxy_redirect default;
}
<!-- -->
- cmd line: ./path_test ---docroot=. ---deploy-path=/test ---http-address=127.0.0.1 ---http-port=8080
RESULT= working good when test.js and test.css in /path_test directory but when i put these in Js and Css directories wt cannot resolve relative path.
Code:
std::cout << "RESOLVE_RELATIVE_URL=" << resolveRelativeUrl("Css/test.css") << std::endl;
useStyleSheet("Css/test.css");
require("Js/test.js");
Output: RESOLVE_RELATIVE_URL=/Css/test.css
Here is directory hierarchy;
/path_test
|---- application.h
|---- application.cpp
|---- main.cpp
|---- /Css
|---- test.css
|---- /Js
|---- test.js
I tried to explain problem. I really tried all variations. I set baseUrl in wt_config.xml to different varitions. I couldnt fix this problem.
I really need help or advise. I stucked.
Replies (7)
RE: relative path resolution problem. - Added by Garrett Mc over 13 years ago
Emir,
Just a quick thought, have you tried changing your docroot from a relative '.' to the fully qualified path?
-Garrett
RE: relative path resolution problem. - Added by Emir Cem over 13 years ago
Hello Garrett,
Thank you for your interest. After your advise, I set docroot to absolute path but not working.
I set to following values,
cmd line: ./path_test ---docroot=/Archive/Projects/QtCreatorProjects/path_test ---deploy-path=/test ---http-address=127.0.0.1 ---http-port=8080
cmd line: ./path_test ---docroot=/Archive/Projects/QtCreatorProjects/path_test/ ---deploy-path=/test ---http-address=127.0.0.1 ---http-port=8080
But not working..
RE: relative path resolution problem. - Added by Emir Cem over 13 years ago
I tested with absolute path in source file..
useStyleSheet("/Archive/Projects/QtCreatorProjects/path_test/Css/test.css");
require("/Archive/Projects/QtCreatorProjects/path_test/Js/test.js");
Not working.. This message has appeared..
Fatal error: failed loading /Archive/Projects/QtCreatorProjects/path_test/Js/test.js
RE: relative path resolution problem. - Added by Garrett Mc over 13 years ago
If your using QtCreator you have to account for the fact that it uses different build directories than source directories. ie, http://doc.qt.nokia.com/qtcreator-2.4/creator-build-settings.html
RE: relative path resolution problem. - Added by Emir Cem over 13 years ago
Hello Garrett,
I removed all *.o files and compiled with g in command line. There is same problem. There's no difference
cmd:g -o path_test application.cpp application.h main.cpp -L/usr/local/lib -lwt -lwthttp
I run like this:
./path_test ---docroot=/Archive/Projects/QtCreatorProjects/path_test ---deploy-path=/test ---http-address=127.0.0.1 ---http-port=8080
This message has appeared again.
Fatal error: failed loading /Archive/Projects/QtCreatorProjects/path_test/Js/test.js
Thanks for your interest.
RE: relative path resolution problem. - Added by Garrett Mc over 13 years ago
This is still screaming of being a basic path problem. I'd confirm that all the files your looking for are actually in the location they are being requested from.
RE: relative path resolution problem. - Added by Koen Deforche over 13 years ago
Hey,
You are mixing up what Wt does, the browser does, and nginx does.
Wt generates paths --- relative by default (for all the good reasons relative paths offer), unless Wt has established its public deployment URL (through JavaScript)
in which case it will generate absolute paths, because HTML5 history usually gets in the way of relative paths.
The browser converts a relative path to an absolute path when it does a request (using the 'document URL' as the basis against which the path is resolved).
Next, Nginx receives a request. In your third try scenario, this would be http://myserver/test.css. But you've configured your proxy to only pass http://myserver/test\[/...\] to the Wt application, so nginx will use its default strategy which is to look for this file in its document root, where it is not found and hence the 404 (resource not found).
How should you solve this ? You have a few options:
# Let nginx serve the static files. That's what nginx is made for. You thus copy the CSS/JS/... into nginx's document root.
# Make sure that you put reverse proxy forwards for every possible URL that needs to be handled by Wt's httpd in nginx. This could be more convenient since you then can keep the binary and static files together (but that's arguably a big benefit)
Regards,
koen