Nginx serve images from multiple static directories under one alias - windows

I am making transition from apache to nginx, and i can not get the alias to serve images. Reading nginx documentation, diving in google and what not have not yielded any acceptable results for me ;(
The images is located C:/xampp/htdocs/content/XYZ/thumbn/image.jpg
the XYZ part is dynamic and changes.
Nginx is set up to serve content from root C:/nginx/public_html/domain.dev/
In the apache i had simply made Alias /CDN "C:\xampp\htdocs\content"
So that way when ever i request www.domain.dev/CDN/XYZ/thumbn/image.jpg i would get the image from the alias directory.
So im trying to replicate the same in nginx
location /CDN {
alias C:/xampp/htdocs/content;
autoindex on; # just there to see if it works.
}
Unfortunately when i access the image www.domain.dev/CDN/XYZ/thumbn/image.jpg i get 404. In the Nginx logs i see following record :
2017/07/04 16:43:32 [error] 10532#6488: *4 CreateFile() "C:/nginx/public_html/domain.dev/CDN/XYZ/thumbn/two.jpg" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: www.domain.dev, request: "GET /CDN/XYZ/thumbn/two.jpg HTTP/1.1", host: "www.domain.dev"
it is how ever listing the directory contents.
Now if i change the location block to this
location ^~ /CDN/ {
alias C:/xampp/htdocs/content/;
}
I get the same error in log, only way i managed to get it working (but not in the way i need) was when i use this location block
location ^~ /CDN/ {
alias C:/xampp/htdocs/content/XYZ/thumbn/;
}
Then it is showing the image, but the url for the image is different and not valid for my project.
Now the question: how do i serve only images from multiple static directories under same alias?
the images are located in following structure
C:/xampp/htdocs/content/one/thumbn/image.jpg
C:/xampp/htdocs/content/two/thumbn/cats.jpg
C:/xampp/htdocs/content/three/thumbn/dogs.jpg
C:/xampp/htdocs/content/another_random_direcotry/thumbn/random.jpg
And they should be accessed by URL as follows
www.domain.dev/CDN/one/thumbn/image.jpg
www.domain.dev/CDN/two/thumbn/cats.jpg
www.domain.dev/CDN/three/thumbn/dogs.jpg
www.domain.dev/CDN/another_random_direcotry/thumbn/random.jpg
Is this even possible with Nginx? so how do i achieve this? Is regex required in this occasion?
full config file here

try this
location /CDN/ {
alias C:/xampp/htdocs/content/;
}

The problem was caused by
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 7d;
}
When i comment out this location directive i can access the images from alias directory.
This how ever removes expiration header for images and with this enabled i can not access the images.

Related

how can i set was: SpringBoot(tomcat) + web: nginx(bring the thymeleaf resource files from was) on docker in linux

It was a project with a base of springboot + thymeleaf.
I want to use the web server by using nginx to place the resource files of thymeleaf on the web server.
Running nginx and spring boot project (WAS) with docker container.
nginx uses a port number of 8003:80 and WAS 8002:8080.
At this time, I want to know the settings of nginx.conf and the settings of the application.yml file of WAS.
In the linux environment, there is a situation where mapping is not working properly depending on whether or not thymeleaf is "/", so I want to solve this.
The settings for nginx.conf are as follows.
server {
listen 80;
location / {
proxy_pass http://ipaddr:8002;
...
}
location /templates {
root /usr/resources/templates;
}
location /css/ {
alias /usr/resources/static;
}
}
The application.yml setting for WAS is as follows.
thymeleaf:
prefix: /usr/resources/templates/
suffix: .html
Some are omitted, but by default, they refer to the directory in the same way as above.
When you run each server, the was server and nginx seem to be calling normally, but they don't seem to map the screen that will eventually be displayed properly.
[TemplatesInputException: Error resolving template [common/pagename], template might not exist or might not be accessible by any of the configured Template Resolves]
An error is occurring. Please Help me.
I tried to modify the nginx.conf file in nginx and the application.yml file in was server in many ways.

Where should the passwords file be located for nginx

I am following the elasticsearch nginx integration tutorial for windows. I have generated the password using openssl.
The question is what should be the extension for the passwords file and where should it be placed.
I keep getting this error message but it is very unclear to me what exactly the issue is
C:\Program Files\nginx-1.12.1\nginx-1.12.1>nginx -s reload nginx:
[error] OpenEvent("Global\ngx_reload_4428") failed (2: The system
cannot find the file specified)
Currently, the file is present inside the configs directory
events {
worker_connections 1024;
}
http {
upstream elasticsearch {
server 127.0.0.1:9200;
}
server {
listen 8080;
auth_basic "Protected Elasticsearch";
auth_basic_user_file passwords;
location / {
proxy_pass http://elasticsearch;
proxy_redirect off;
}
}
}
You have to specify full path for the password file location:
location / {
auth_basic "Secure Area (or whatever description you want)";
auth_basic_user_file /etc/nginx/auth/nginx.passwd;
... (other settings)
}
The example above works on Unix/Linux servers running nginx. Since you're running Windows I suppose you have to specify full path like C:\Program Files\nginx-1.12.1\nginx-1.12.1\nginx.passwd
Depending what exactly you want to restrict you might need to place the rule inside or outside of the location / {} block. Supposing you need to allow full access to your server/site and only restrict let's say /private then you will add the basic auth in:
location /private {
...
}

Nginx geo ip module doesn't work, cannot find a way to debug

I follow nginx geoip configuration guide here http://www.howtoforge.com/nginx-how-to-block-visitors-by-country-with-the-geoip-module-debian-ubuntu
I write something like this
geoip_country /path/to/GeoIP.dat;
I am pretty sure the .dat file is there, and nginx has permission to access it. However, the geoip_country_code variable seems not set.
I've tried many approaches to debug, like
add_header X-debug-message "$geoip_country_code";
or
log_format debug "$geoip_country_code"
for header, nginx returns empty result. And for log format, it simply record nothing. My bet is geoip_country_code is not even defined, so nginx cannot deal with it or what.
I tried to read the server error log, however, nothing there (nothing about geoip and the undefined variable). It's kinda annoying, how can I know what's going on with nginx and the geoip module?
After looking into syslog, I noticed that it's a segfault in geoip module
Aug 10 12:55:39 web kernel: [1183521.905522] nginx[30201]: segfault at 7fa773b7f2ce ip 00007fa772bc5478 sp 00007fffe4adc570 error 4 in libGeoIP.so.1.6.0[7fa772bbe000+2d000]
So, I bet it's caused either by geoip module or corrupted GeoIP.dat file. After looking into the file, I found that it's corrupted. Then I download the file again, everything works fine now.
A way to debug could be checking the environment for the GEOIP variables, e.g. in PHP look in the $_SERVER array, or try getenv(GEOIP_COUNTRY_CODE).
Make sure to define the variables in /etc/nginx/fastcgi_params:
### SET GEOIP Variables ###
fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
source

WordPress does not go to correct address to find a file

I am running WordPress on my localhost and trying to send request from a file of my plugin to another file called photos.php using ajax, I have correctly written the address of file in xmlhttp.open function but it runs into following error.
Not Found
The requested URL /wordpress/wp-admin/wp-content/plugins/myphoto/photos.php was not found on this server.
the actual address of the file is /wordpress/wp-content/plugins/myphoto/photos.php
xmlhttp.open("GET","wp-content/plugins/myphoto/photos.php?c="+option,true);
xmlhttp.send();
Try setting the absolute path:
xmlhttp.open("GET","/wordpress/wp-content/plugins/myphoto/photos.php?c="+option,true);
Your error implies you're using the relative path. Directory /wordpress/wp-admin/wp-content... does not exist.

nginx Windows: setting up sites-available configs

I'm trying to set up Nginx on my Windows development environment. I can't find how to create something similar to "sites-enabled" on Linux where Nginx would look for (links to) active virtual host configurations.
Is there a way to do something similar with a directory with shortcuts to the actual configuration files and Nginx scanning that directory? Or is there another way to hook up a virtual host configuration other than copying the host configuration to nginx.conf?
In windows you have to give full path of the directory where the config files are located. There are two files to update: nginx.conf, which tells nginx where to find web sites, and localhost.conf, which is the configuration for a web site.
It is assumed that nginx is installed in C:\nginx. If the installation directory is at another path, you will have to update that path accordingly, wherever it appears in the following two configuration files.
nginx.conf
Location: C:\nginx\conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#to read external configuration.
include "C:/nginx/conf/sites-enabled/*.conf";
}
localhost.conf
Location: C:\nginx\conf\sites-enabled
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
The "sites-enabled" approach as used by some Linux packages of nginx utilize include directive, which understands shell wildcards, see http://nginx.org/r/include. You may use it in your own config as well, e.g.
http {
...
include /path/to/sites/*.conf;
}
Note though that such approach might be very confusing (in particular, it would be hard to tell which server{} is the default one unless you use default_server explicitly).
The following worked for me but only AFTER I moved my main nginx exe folder from c:/Program Files (x86)/nginx-1.7.0 to c:/nginx-1.7.0 (because I think it doesn't handle spaces in file paths well):
http {
...
include "f:/code/mysite/dev-ops/nginx/dev/mysite.conf";
}
Till the time of writing this answer (7 Jan 2022) non of the other answers fully answer this question.
Wildcards (include a/*.b) just include a list of vhosts which cannot be disabled/enabled. sites-enabled and sites-available is about being able to disable a vhost without deleting the corresponding config file.
Nginx has only one config file (nginx.conf), which in turn includes other files. The ability to include files is what led to enabled/available design.
So the directory structure goes as follows:
conf // or whatever
|__nginx.conf
|__sites-enabled
|____default // symlink to sites-available/default.conf
|__sites-available
|____default.conf // You can omit the extension but I just like it
|____whatever.conf
|____some vhost.conf
|____another vhost.conf
|____disabled vhost.conf
|____other config files ...
# nginx.conf
http {
# ...
include path/to/sites-enabled/*; # include the enabled ones
}
In windows (cmd) you do:
mklink Link Target
# for example
mklink default X:/path/to/nginx/conf/sites-available/default.conf
Many think that windows doesn't have symlinks, it does :-)
I use a slightly more complex config directory structure, for development:
conf/
|__nginx.conf
|__sites-enabled/
|____some-site // sites-available/some-site/{env} where {env} is either dev or prod
|__sites-available/
|____some-site/
|______prod.conf
|______dev.conf
|______dev/
|________www.conf // vhost (server {}) for the www subdomain
|________api.conf // same as above but for the api subdomain
|________root.conf // vhost for the top level domain (e.g example.com without any subdomain prefix)
|______prod/
|________www.conf
|________api.conf
|________root.conf
|______snippets/
|________http.conf // listen on ipv4 80/ipv6 80 and redirect http to https
|________https.conf // listen on ipv4 443 ssl/ipv6 443 ssl and `include`s ssl.conf
|________ssl.conf // ssl config, pay attention to permission
You can include your config with a relative path in your nginx.config (the relative path is just the path of the config file itself in contrast to the logs path for example):
http {
include mime.types;
default_type application/octet-stream;
include ../sites-enabled/*.conf;
...
}

Resources