I want to write a config file for an nginx virtual host that looks like this:
server {
listen 80;
server_name www.my-domain-name.com;
access_log /home/me/sites/$server_name/logs/access.log;
error_log /home/me/sites/$server_name/logs/error.log;
location /static {
alias /home/me/sites/$server_name/static;
}
location / {
proxy_pass http://localhost:8000;
}
}
Using $server_name seems to work find for the location /static, but it doesn't seem to work for the access_log and error_log -- am I doing something wrong? Or is this just not possible? Can I do it some other way?
[update] - this is the error message when trying to reload nginx:
nginx: [emerg] open() "/home/me/sites/$server_name/logs/error.log" failed (2: No such file or directory)
I wanted to do this too, but apparently by design nginx cannot expand variables in the error_log command, in case there are errors doing so and it cannot get a log filename to write them to.
Their suggestion is to use some program to generate your configuration files instead. You could use sed for this, to automatically search-and-replace your own variables and placing the output in the nginx configuration directory.
Related
as the headline said, my nginx isn't able to find .htpasswd-protected directories. Here are my settings, my .conf and the locations of the relevant files:
NOTE: due to my brothers wish, the nginx runs on a win8.1x32 virtual machine
I created a .htaccess file in my root/xx directory looking like this:
AuthName "Restricted Area"
AuthType Basic
AuthUserFile /b/.htpasswd
AuthGroupFile /dev/null
require valid-user
And I created a .htpasswd file inside the shown directory.
After that I modified my .conf in the conf-folder like this:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location ^~ /b/ {
auth_basic "Restricted Area";
auth_basic_user_file b/htpasswd;
}
}
I deleted the comment-lines from the auto-generated file only here, in the original, they are still existent - thought this might clear your sight on the file.
Now, here comes the problem: the file which I am aiming for is called int.html and can be found in /b/int.html (stylesheets and scriptfiles included), but every time i want to get to this file, the browser asks my to type in password and username - as I wanted it - but then the browser shows a 404-error, which means, he couldn't find the named file. (I made sure everything is spelled correct in the directories and tried this in several browsers on different devices). I don't know why. (I already tried to set the int.html as index in the config like this:
location ^~ /b/ {
auth_basic "Restricted Area";
auth_basic_user_file b/htpasswd;
}
But it still doesn't work. - Ideas, anyone?
Greetz Dyarn
Using Nginx, I'm trying to configure my server to accept all domains that point to the IP of my server, by showing them a specific website, but when accessing the www.example.com (main website), I'd show an other content.
Here's what I did so far:
server {
// Redirect www to non-www
listen 80;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
server {
listen 80;
server_name example.com;
// rest of the configuration
}
server {
// Catch all
listen 80 default_server;
// I also tried
// server_name _;
// Without any luck.
// Rest of the configuration
}
The problem with this configuration is that every request made to this server not being www.example.com or example.com is took under example.com server configuration, not the catch all.
I'd like to cath only www.example.com/example.com in the first two configurations, and all the others in the last configuration.
I suggest putting your server on top of the file :)
I think nginx wants default servers to be on top of -a- file.
I have really much files on my server, but there is one with a default server as first server declaration, and that works.
I am completely new to nginx and I am asked to find a way to serve Map Tiles that are separated according to the zoom levels. The image file structure is like ~/data/images/7/65/70.png where 7 is the zoom level, 65 and 70 are the lon-lat values. The folder 65 contains many files such as 71.png, 72.png and etc.
I have installed Nginx properly and I can get Welcome to nginx message. I have followed the instructions in http://nginx.org/en/docs/beginners_guide.html and created the /data/www and /data/images directories. I have placed index.html file under /data/www and tile images under /data/images. Then I modified the configuration file by adding following lines in http tags:
server {
location / {
root /data/www;
}
location /images/ {
root /data;
}
}
After reloading the config file and entering localhost on the browser I can neither get the index.html file nor see the images.
What I am trying to do is to display the image when I enter something as:
http://localhost/1.0.0/basemap/7/65/70.png
7: folder indicating 7th zoom level
65: folder indicating the latitude
70.png: file indicating the longitude (folder 65 includes many png files)
What am I missing?
Ok, let me explain something, you already have a localhost server, which is defined inside a file called default that is the file that causes the "Welcome to nginx" or something to appear, and I believe you can't create a new server with the same server_name, let's remove that and make your localhost serve only those images,
First we need to delete the default file from sites-enabled , it will still exist inside sites-available if you ever want to get it back. ( note that all files inside sites-enabled are simply symlinks from the files inside sites-available )
We create a new file inside sites-available and call it whatever you want, images-app for example
create the new server inside the images-app file, I'll assume that the root of the app is inside a folder called /data of course you will map that to your own server structure.
server {
server_name localhost;
root /data;
index index.html;
location / {
try_files $uri =404;
}
}
now we go to sites-enabled and enable this site we created inside sites-available
sudo ln -s /etc/nginx/sites-available/images-app /etc/nginx/sites-enabled/
make sure that all the nginx config are correct
sudo nginx -t
If nothing is wrong we can go ahead and reload nginx settings
sudo service nginx reload
For my case I just edited /etc/nginx/sites-enabled/default file.
I added following config:
location /images/ {
root /data;
}
and placed images under /data/images:
and url works: http://localhost/images/example.png
I use VS Code as SuperUser. (I know it is bad, but I accept risks)
It helps a lot with root access file editing:
I'm also new to nginx, Here is my solution that is similar with Mohammad AbuShady's answer :
delete sites-enabled/default
create the whatever.conf in /etc/nginx/conf.d/
The reason is:
sites-enabled/default has defined a server
that is listening on 80 rooting with /var/www/html
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
the nginx.conf file includes other conf files
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
pay attention on permission
the 1st edition of my.conf is root on /home/scotv/, but will get 403 Forbidden error, check the error.log:
2016/04/07 20:12:44 [error] 12466#0: *2 open() "/home/scotv/data/a" failed (13: Permission denied),
client: 127.0.0.1, server: , request: "GET /a HTTP/1.1", host: "localhost"
I just installed nginx and php fastcgi about an hour ago, and after reading examples of a quick starting configuration, and the nginx documentation etc, I just cant get it to work.
No matter what I change or try, I always only get the "Welcome to Nginx!" screen on "localhost/..." - I cant even call a simple index.html
My config:
(the stuff in the comments is what I tried out)
// default nginx stuff (unchanged)
server {
#listen 80 default_server;
#listen 80 default;
listen 80;
#server_name localhost;
#server_name _;
#access_log /var/log/nginx/board.access_log;
#error_log /var/log/nginx/board.error_log;
#root /var/www/board;
#root /var/www/board/public/;
root /var/www/board/public;
#index index.html;
index index.html index.htm index.php;
}
If I understand it right, this should be the easiest setup, right? just define listen 80; and index index.html; but I just cant get it to work
The file /var/www/board/public/index.html exists and has content
Before I waste 2 more hours trying out something, can someone of you give it a quick watch and tell me what I'm doing wrong? Thanks.
Fundamentally you hadn't declare location which is what nginx uses to bind URL with resources.
server {
listen 80;
server_name localhost;
access_log logs/localhost.access.log main;
location / {
root /var/www/board/public;
index index.html index.htm index.php;
}
}
I'm googling a lot and found several workarounds, but you have to define every single directory.
On Apache: example.com/hi -> example.com/hi/
On nginx: example.com/hi -> Firefox can't establish a connection to the server at example.com:8888
where 8888 is what Apache is listening on (nginx's :80 -> localhost:8888)
Any ideas how to fix this and have it just forward normally like folder?
I had a similar problem with varnish and nginx (varnish on port 80 proxying to nginx listening on 8080) and needed to add "port_in_redirect off;" ... server_name_in_redirect needed to stay on so nginx knew which host it was handling.
The following should do the trick, but it needs more thought/work, because only a single location block will get used at a time:
location ~ ^(.*[^/])$ {
if (-d $document_root/$1) {
rewrite ^(.*)$ $1/ permanent;
}
}
(not tested)
You can set "server_name_in_redirect off" on your server section
server{
listen 80 default;
server_name localhost;
server_name_in_redirect off;
...
...
}
That will do the trick ;-)
HTH.
Edit: Just format.
This is the magic that works best for me:
try_files $uri $uri/ #redirect;
location #redirect {
if ($uri !~ '/$') {
return 301 $uri/$is_args$args;
}
}
The 'if' statement here is safe per: http://wiki.nginx.org/IfIsEvil