nginx serve a specific directory for a specific location - nginx

I need for development purpose (not for production of course), to serve my logs that are in my folder /app/logs/.
So I've setup a configuration like this :
server {
listen 80;
server_name server-name.com;
error_log /app/logs/error.log warn;
access_log /app/logs/access.log compression;
root /var/www;
location /info/logs/ {
alias /app/logs/;
autoindex on;
}
location / {
// others route working
}
}
But everytimes i try to access to something like /app/logs/django.log using http://server-name.com/info/logs/django.log, I get a 404 and not the file I asked for.
I've tried many things like chmod -R 755 the entire folder or setting the folder to the user nginx use (in my case for now root, I know it's bad), tried root or try_files but I just can't access it...
I've seen many topics here and there but can't find a clue...
Can you help me with this please ?
PS : I need the root /var/www at the beginning for others locations.
PS2 : I'm using a Docker based on Debian 9.

If you are using the standard NGINX Docker container, then the default configuration is to send the Access and Error logs to the Docker log collector. Not the standard log output for NGINX.
The result is that your log files are most likely going to:
file/var/lib/docker/containers/<container id>/<container id>-json.logon the Docker Host. Where <container id> is the long-form version of the Container Id specified when the container was setup in the first place.
To get to the default log files you can do: docker logs <container name>
To find the <container id> you can do: docker inspect --format '{{ .Id }}' <container name>
If you want to customize where the log files go, then you need to create a helper Docker container that the log files can be written too.

Ok, I've found my problem, I had a route like this :
# Any route containing a file extension (e.g. /devicesfile.js)
location ~ ^.+\..+$ {
try_files $uri =404;
}
And I didn't know that despite the fact it was after the first location, it would be choosen over the other location...
Thanks for your help anyway !

Related

Simple nginx config not working on CentOS

I have installed Nginx 1.12.2 on CentOS 7. I have an extremely simple nginx config and it is not working at all. I have setup several nginx instances on Ubuntu in the past without any issue I wonder if there is something to do with CentOS.
I have double-checked that the "root" directory exists and the files also exist with proper permissions. But I am getting 404 error. Also for debugging purpose, I tried to put "return 200 $uri" in the location block and it seems to be returning me the proper URI but try_files doesn't work
/var/www/mydomain/public/test.html exists with proper permissions
For debugging when I put "return 200 $uri" it shows up when I hit the domain
Hitting mydomain.com/test.html gives 404
server {
listen 80;
root /var/www/mydomain/public;
index index.html index.htm;
server_name mydomain.com;
location / {
# return 200 "$uri";
try_files $uri $uri/;
}
}
Few things:
Check your NGINX error log at /var/log/nginx/error.log, you will likely see what file is being accessed and make conclusions from that
Be aware of the presence of /etc/nginx/conf.d/default.conf, which is shipped with the package. It has default server, which is what NGINX will use when no domain has matched, however it's a sample file rather than a real config. I tend to just echo > /etc/nginx/conf.d/default.conf, to "remove it" in a safe way. (If you just remove the file, then package update will restore it, but if you nullify it like I do, then package upgrades won't touch it).

Nginx can't serve static content and return 403(Forbidden) error page

I am new to nginx and trying to serve static contents with nginx and getting 403 error.I have server config like this:
server {
listen 8000;
server_name localhost;
root /Users/ismayilmalik/Documents/github/nginx-express;
location / {
index index.html;
}
I have executed commands below:
chmod -R 755 /nginx-express
chmod -R 644 /nginx-express/*.*
And the folder has drwxr-xr-x rigt.What's wrong here?
Please go to your nginx error logs to get details.
Run this command to show last errors:
tail -20 /var/log/nginx/error.log
It's good to go through error logs located /var/log/nginx/***.error. I had problems similar to this once. The solution was the user nginx was running as.
If nginx is running as www then www will not have access to ismayilmalik folders unless you also grant access to /Users/ismayilmalik home folder, but that is not secure. The best solution would be to allow nginx to run as ismayilmalik if you want to access your home folder through nginx.
I solved it finally.Actually nginx had all permission to serve static content from:
/Users/ismayilmalik/Documents/github/nginx-express;
The reason was when started nginx could not create error.log file in it'sroot directory. After manually creating the file it worked fine. I am using macOs and to find logs folder executed the command below to find all enironment variables for nginx:
nginx -V
BTW before this I had changed nginx user to from nobody to admin in main config file like below.
user [username] [usergroup]
By default nginx master process runs under root and child process under nobody.

Nginx server_name regex match not setting passenger_app_env

Nginx: Built with passenger-install-nignx-module
Passenger Version: 5.0.28
OS: Ubuntu 14.04
I have symlinked each of my apps into their own set of environment folders:
/Repository
/development.manager
/app
...
/test.manager
/staging.manager
...
Where the actual folders is at another location on my HDD. All of these folders are symlinks pointing to that one folder.
The problem is that Nginx doesn't seem to be setting the passenger environment variable properly. Checking the logs it throws an app error that doesn't make sense (and the nginx config is the only thing that's changed since things broke). Also, the error page showing states:
Because you are running this web application in staging or production
mode, the details of the error have been omitted from this web page
for security reasons.
Which means that it's not using the development environment even though the root directory in the logs shows development.manager. This is when I access through the url: http://manager-development/.
Here's the relevant excerpt from my nginx sites-enabled configuration:
server {
listen 80;
server_name ~^manager-(?<environment>development|test)$;
passenger_app_env $environment;
passenger_ruby /home/vagrant/.rvm/gems/ruby-2.3.1#manager/wrappers/ruby;
passenger_enabled on;
root /home/vagrant/apps/$environment.manager/public;
client_max_body_size 30M;
}
I have a feeling the solution might be a combination of an answer I provided here as well as a possibly misconfigured nginx block.
EDIT: I explicitly raised an error in my rails app that output the environment as a string and it's literally "$environment"...
I've given up on this approach as it seems variables aren't interpreted by nginx when used in certain places. I'm now using a custom Bash/Ruby script to iterate over my environments/app names and generate the configuration blocks.

nginx: create directory if it doesn't exist

I'm new to nginx and I have a given nginx config.
There is a mapping like:
map $http_host $my_customer {
default "default";
"~*cust1" "cust1";
"~*cust2" "cust2";
}
And there is the access_log line:
access_log /my/log/path/access.log
Now I want to have separate log-directories and log-files for each customer, so I changed the access_log line into:
access_log /my/log/path/$my_customer/access.log
This works fine if the $my_customer-directory already exists. But if it doesn't exist, then nginx does not log. I know how I can check if the directory exists:
if (!-d /my/log/path/$my_customer) {}
But how is it possible to create a directory inside the nginx config file?
In order to start nginx process all directories have to be created in advance.
The owner of dir should be the user used by worker processes defined in nginx configuration file (/etc/nginx/nginx.conf by default).
The user should have write permissions to this directory.
As #Alexey Ten noticed, it is a good practice to use default logs location:
/var/log/nginx/$my_customer.access.log
Otherwise, you have to do something like that:
mkdir -p -m 755 /my/log/path/$my_customer

Nginx keeps showing Welcome to Nginx

I am using Cent OS 6.1.
I installed Nginx by ./configure method from source. I started the nginx server by sudo nginx and it can serve the Welcome to Nginx page.
However, when I edit the /usr/local/nginx/conf/nginx.conf file, I found that changing the ...location / {... }... block has no effects.
For example, changing
location / {
root html;
index index.htm index.html;
}
to
location / {
root xyz123; #which does not exist
index index.htm index.html;
}
should give 404. But it keeps showing the welcome page.
Even I remove the whole location block, it still shows the welcome page. But if I change the /usr/local/nginx/html to /usr/local/nginx/htmlxyz it shows 404. Is there another conf file running that overridden the nginx.conf?
p.s. I did sudo nginx -s stop then sudo nginx or sudo nginx -s reopen but didnt help :(
Why you install over EPEL. I've installed 10 nginx server just like that and it is working with Node.js.
Maybe you can get some error ? Show full ./configure.
Example Nginx / Php / Mysql for Centos :
https://www.howtoforge.com/installing-nginx-with-php5-and-php-fpm-and-mysql-support-on-centos-6.4
Default centos nginx html path :
/usr/share/nginx/html/

Resources