How to serve file under root path using Nginx? - nginx

I have a website laike9m.com, which is served by Nginx. I want to access a text file AAA.txt with url laike9m.com/AAA.txt. The text file is on my server, let's say it's $HOME/AAA.txt. Redirection is not allowed. Current Nginx conf file is here. Thank you.

location ~ ^(.*\.txt)$ {
alias /home/laike9m/$1;
}
Solved it.

Related

nginx serve file but not all files in dynamic directory

i want to serve file on nginx server here's the case below
serve location /home/ubuntu/username/fileName
eg1 /home/ubuntu/john/d1.txt
eg2 /home/ubuntu/john/d2.txt
eg3 /home/ubuntu/kiddo/d3.txt
it is not accessable using.
location ~ /home/ubuntu/(?<user>.+)/(?<file>.+)$ {
root /home/ubuntu/$user/$file;
}
nginx rules on stackoverflow
Could you please try following, based on your shown samples. Please make sure to clear your browser cache before testing your URLs.
location ~ /home/ubuntu/([^/]*)/([^/]*)/?$ {
root /home/ubuntu/$1/$2;
}

Expose files to public via rewrite

I'm using nginx docker engine for a symfony app, and I have some images under /var/www/html/project/ezplatform/files/(.*) that i need to expose them to the public.
I tried to add under my nginx configuration this rewrite config:
rewrite "^/var/www/html/project/ezplatform/files/(.*)" "/app.php" break;
rewrite "^/var/www/html/project/ezplatform/files/(.*)" "/var/www/html/project
/ezplatform/files/$1" break;
But still facing an exception 404 Not Found when I try to access to an available image.
Any idea would be appreciated.
Try adding a location block to your nginx conf, where you define a handle for the images directory, which nginx then uses to retrieve files from the full path. Like:
location /other_images/ {
alias /var/www/html/project/ezplatform/files/;
}
You can link to the images using your 'virtual' folder path:
other_images/example.gif
(So in HTML, <img src = "other_images/example.gif">, or use whatever the symfony syntax is for image links.)
Let me know if this solves the 404 Not Found problem.

NGINX CSS Not Loading

I just set up my new vps to hold my website server. It works perfectly fine on my existing vps, but after I put it on the new one, none of the CSS files are loading.
Image here:
Any help is appreciated thanks!
In your server block in there has to be location for static files:
server {
server_name yourhost.com www.yourhost.com;
root /var/www/vhosts/yourhost.com/public/;
...
location /static {
alias /var/www/vhosts/yourhost.com/public/static;
}
}
So that
location /static {
alias /var/www/vhosts/yourhost.com/public/static;
}
has to be properly configured, pointing to the location where your static files are placed.
Then if your css is in file styles.css (or any other file) and is placed on the server as /var/www/vhosts/yourhost.com/public/static/css/styles.css, it will be accessible from /static/css/styles.css in your html.
To see where exactly this is configured, you can start with nginx.conf (normally in /etc/nginx/nginx.conf) and check what other conf files are imported.
Note the path provided is fully up to you, but once it's known it has to be properly configured with the alias in the static location.

run cgi script using nginx

I am trying to execute cgi script on Nginx. In nginx.conf file, I added a location directive such as below:
location /cgi-bin{
root cgi-bin;
index index.cgi;
}
When I try to connect to http://example.com/cgi-bin/index.cgi, it says file not found. In error.log, I see the request as "http://example.com/html/cgi-bin/index.cgi.
cgi-bin is not in html folder. The correct path is "http://example.com/cgi-bin/index.cgi
I tried many possibilities for location directive. Either it looks in html/cgi-bin or it does /cgi-bin/cgi-bin/index.cgi. I am not sure why it uses 'cgi-bin' twice
Any suggestions.. I have been trying for hours now!!!

configuring nginx server to store the static images and html

I want to configure the nginx server in my windows7 PC.For storing images and html files.
I followed up the following steps:
1.I downloaded the nginx-1.2.9 and unziped into c:\ filder.
2. created one folder "data" and within 'data" folder created another two folders say "WWW" and "images".
3.Keeping all images in the "images"folder .and .html file in folder "WWW".
4.Now started the nginx server using command C:\nginx-1.2.9>start nginx
5.Made changes in nginx.conf file.`
#server {
#location / {
# proxy_pass http://127.0.0.0:8080;
#}
#location /images/ {
# root /C:/data/images;
# }
}
Not able to access images and html page.
Please help me to solving this problem. I'm sure doing mistake in config file only..
Thanks in Advance,
Satya
You have commented the configuration data.First remove all the # from your configuration file.Then use the below code inside the server {}
location / {
root data/www;
}
location /images/ {
root data;
}
Note- the location of the static file inside your nginx root folder should be the (root+location) data and the access of the file should be "location" data. e.g from the first location configuration the static file should present inside the folder "data/WWW/" and in the second location configuration the static file should present inside the folder "data/images/".
URL folder inside nginx home path
----- --------------------------
localhost/hello.html data/WWW/hello.html
localhost/images/img1.png data/images/img1.png

Resources