Quantcast
Channel: Nginx Forum - How to...
Viewing all 2931 articles
Browse latest View live

How to make nginx to redirect multiple subdomains? (no replies)

$
0
0
How to make nginx to redirect multiple subdomains? Including non-www redirects to the domain with www.
For example, my website is abc.com, under the site has several directories aaa, bbb, ccc, ddd, DNS has been set up correctly, all point to the same IP. How to meet the following requirements:
Enter abc.com automatically redirected to www.abc.com
Enter aaa (or bbb, ccc, ddd) automatically redirected to the corresponding web directory, such as aaa. abc.com then displays the contents of the web root directory /aaa, while the address bar unchanged?
If you enter aaa.abc.com/aaa or www.abc.com/aaa returns 404 (as it has been redirected to the aaa directory, but no aaa aaa following directory, so returned 404)

Cannot even start nginx on windows (4 replies)

$
0
0
Hi all,

I cannot start nginx on Windows.

I successfully installed nginx at home on a debian wheezy (the "raspbian", in fact, on Raspberry Pi)... but I just followed a tutorial and I am still a newbie!

I am trying to reproduce this at work, on my professional PC (Windows 7): I am prospecting this way for my project too :-)

I downloaded nginx 1.5.12 from the download page, unzipped it as c:\nginx\nginx.exe (with its work folders aside, as in the zip).

Without configuring anything, I double cliqued on nginx.exe and got nothing (except one very short blink of the application console, I guess).

I then opened a DOS window to launch the server, and got the following errors:

D:\Users\phil>c:\nginx\nginx.exe
nginx: [alert] could not open error log file: CreateFile() "logs/error.log" failed (3: FormatMessage() error:(15100))
2014/04/03 11:30:01 [emerg] 2356#6140: CreateFile() "D:\Users\phil/conf/nginx.conf" failed (3: FormatMessage() error:(15105))
D:\Users\phil>

(see joined bmp file)
Note that the first error is also visible in the Windows EventLog

Can somebody help me?

Thanks in advance,
Philippe.

Redirecting folder to root with rewrite (no replies)

$
0
0
I'm new to Nginx and try to redirect a directory for jpg-files to the root directory. For example the file /abc/jpg.jpg should be opened directly through /jpg.jpg.

rewrite /(.*(jpg|jpeg))$ /directory/$1 last;

This works, but so all jpg files in other directories can't be opened any more. Many greetings

regex to redirect multiple domain only works if domain not declared elsewere (no replies)

$
0
0
Hello, I encounter a strange problem while trying to redirect multiple similar domains. I explain.

Imagine this DNS config:

lollipop A 127.0.0.1
chocolate A 127.0.0.1
*.lollipop CNAME lollipop
*.chocolate CNAME chocolate

Imagine this Nginx config:

# www.domain → domain
server {
listen 80;
server_name "~^www\.(?<domain>.*)$";
return 301 $scheme://$domain$request_uri;
}

# proxy_pass
server {
listen 80;
server_name .chocolate;
location / {
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:81;
}
}

# fallback
server {
listen 80;
root /var/www/fallback;
index index.html;
location / {
rewrite ^.+$ / permanent;
}
}

What I expect:
-------------------

GET chocolate → proxy_pass
GET www.chocolate → chocolate → proxy_pass

GET milk.chocolate → proxy_pass
GET www.milk.chocolate → milk.chocolate → proxy_pass

GET lollipop → fallback
GET www.lollipop → lollipop → fallback

GET bubblegum.lollipop → fallback
GET www.bubblegum.lollipop → bubblegum.lollipop → fallback

What I get:
--------------

GET chocolate → proxy_pass [ok]
GET www.chocolate → proxy_pass [not ok][does not redirect]

GET milk.chocolate → proxy_pass [ok]
GET www.milk.chocolate → proxy_pass [not ok][does not redirect]

GET lollipop → fallback [ok]
GET www.lollipop → lollipop → fallback [ok][redirects]

GET bubblegum.lollipop → fallback [ok]
GET www.bubblegum.lollipop → bubblegum.lollipop → fallback [ok][redirects]

You see it works for undeclared domain names (handled by the fallback rule after beeing handled by the redirect rule), but not for declared domain names.

However, I write the 'www.domain'→'domain' redirection rule BEFORE the rule handling a specific domain. If I add logging, the redirection log sees nothing in this case, this code is not used.

I have tried that too: (yes I know http://wiki.nginx.org/IfIsEvil )

server {
listen 80;
server_name www.*;
if ($host ~* ^www\.(.+)) {
set $domain $1;
return 301 $scheme://$domain$request_uri;
}
}

But it does the same (i.e. does not work).

I’m using nginx 1.2.1 (Debian).

If Anyone have an idea, do not hesitate to share. ;)

Thanks.

Cannot get auth_basic working (1 reply)

$
0
0
Hi all,

I am using nginx 1.5.12 on windows 7, and I cannot get basic authentication working...

Here is the situation:

- in nginx.conf:
[...]
location / {
root html;
index index.html index.htm;
auth_basic "ngvplatform";
auth_basic_user_file c:/nginx/conf/htpasswd;
}
[...]

- in c:/nginx/conf/htpasswd, nothing but these 2 lines:
phil:YAsIdyZPQf712
daniel:YAsIdyZPQf712

- I did get the encrypted password from the following command (is /this/ the point?):
C:\nginx>openssl passwd -crypt password
YAsIdyZPQf712

Then, the behavior on the web browser (Firefox 28 or IE10) is:

- the authentication window appears, asking for credentials for realm "ngvplatform". Fine :-)

- after entering "phil" as name and "password" as password (which should be legal) this window closes and opens again, asking for the same.

- same behavior if I enter "toto" as name and "password" as password (an unknown user).

- this repeats again and again... I cannot get logged in.

- in the error.log file, I can found many samples of the 2 following different lines:
2014/04/04 18:14:27 [error] 3256#5800: *3 user "phil": password mismatch, client: 172.16.25.211, server: wpmaurisset.eu.lectra.com, request: "GET / HTTP/1.1", host: "wpmaurisset.eu.lectra.com"
2014/04/04 18:15:03 [error] 3256#5800: *3 user "toto" was not found in "c:/nginx/conf/htpasswd", client: 172.16.25.211, server: wpmaurisset.eu.lectra.com, request: "GET / HTTP/1.1", host: "wpmaurisset.eu.lectra.com"

Could you please help me to find where I am wrong?
Thanks in advance,
Philippe.

Rewrite url with "|" character? (1 reply)

$
0
0
Hi,
Been searching and searching but I can't seem to figure this out.

How do I rewrite a URL containing the pipe (|) character? If I try to do it with just a plain pipe then it seems to treat it like a regex OR and match it anytime I hit a url with the text before the pipe, which is obviously not desired. I've also tried to escape the pipe in a traditional regex way, no dice. If I try with URL encoded (%7C) it doesn't match at all.

rewrite ^/some/path/with|some|parameters$ /some/other/path permanent; # => matches anytime the url starts with /some/path/with
rewrite ^/some/path/with\|some\|parameters$ /some/other/path permanent; # => matches anytime the url starts with /some/path/with
rewrite ^/some/path/with%7Csome%7Cparameters$ /some/other/path permanent; # => never matches

Seems like URLs are un-encoded before processing, so %7C will not work. But how do I properly escape the pipe to make this work?

[P] MariaDB + Nginx + Adminer: Howto for working configuration (no replies)

$
0
0
Hi!

I have installed
MariaDB
Nginx
and I want to use Adminer for DB administration.

Can somebody please provide a working configuration for this setup?

THX

Maxmind geoip (netspeed and isp db) (no replies)

$
0
0
Hello,
can someone explain me how to get working geoip isp and netspeed db's from maxmind with nginx? Module ngx_http_geoip_module have config only for country and city db's (isp and netspeed have separate .dat file).

Nginx + Laravel Rewrites (no replies)

$
0
0
Hi folks

I try to setup my nginx with multiple laravel-projects in subdirectories:
- root
-- project 1
-- project 2

For that i conf my nignx as:

// default
server {
listen 192.168.178.10:80;

server_name 192.168.178.10;
root /usr/share/nginx/www;

error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
rewrite_log on;

include /etc/nginx/sites-available/*.conf;

location ~ \.php$ {
include fastcgi_params;
}
}

// Project 1
rewrite ^/project1/index\.php/?(.*)$ /project1/$1 permanent;
location /project1 {
index index.php;
try_files $uri @rewriteproject1;
}

location @rewriteproject1 {
rewrite ^(.*)$ /project1/index.php/$1 last;
}

location ~ ^/project1/(index)\.php(/|$) {
include fastcgi_params;
}

// Project 2
rewrite ^/project2/index\.php/?(.*)$ /project2/$1 permanent;
location /project2 {
index index.php;
try_files $uri @rewriteproject2;
}

location @rewriteproject2 {
rewrite ^(.*)$ /project2/index.php/$1 last;
}

location ~ ^/project2/(index)\.php(/|$) {
include fastcgi_params;
}

If i open 192.168.178.10/project2, it opens index.php from project2.
If i open 192.168.178.10/project1, it opens index.php from project2.

Thanks for your help.

Streaming a live h264 video source? (no replies)

$
0
0
Hi,

I have a process that receives a H264 video call, and I want to stream this video in a html5 <video>
element to a group of viewers.

How do you recommend that I do that?

Thanks,
David

Redirecting urls with query strings via map module (no replies)

$
0
0
Hi all,

I'm a bit new to nginx, and I haven't been able to find documentation about this precise problem I've been having, apologize if this has already been covered:

I set up a static mirror of a Wordpress blog on nginx, but some of the old urls end up looking like "/?p=100" instead of their "pretty" permalinks. So I need to permanently redirect "/index.html?p=100" or "/?p=100" to "/2014/04/blog-post-one-hundred/" so that the old links work.

Since there are only about 150 such URLs, I figured I could make a map to handle this problem, but it's not working and I suspect it has to do with nginx's feelings about query strings and regexes.

How should I go about doing this? I was trying stuff like

map $uri $new {
'/?p=100' '/2014/04/blog-post-one-hundred/';
}

if ($arg_p) {
rewrite ^ $new
}

but obviously that hasn't worked (I see "the rewritten URI has a zero length" in the logs). Any ideas?

Trouble with PHP (1 reply)

$
0
0
I've got nginx working well with Django sites and static content. Now I'm importing some php utilities. There is a folder accessed as http://paulwhippconsulting.com/atwell/ that contains a mix of php, html and javascript files. It is password protected.

I got the password protection working fine and all the files being served but when I add the php location expression to get the php working, strange things happen: I get 403 forbidden for the index.php file and 404 not found for the php and other files.

I've been round the houses trying to set up the right location statement without undue repetition and I got to:

location /atwell/ {
alias /home/paul/atwell;
auth_basic "Please log in";
auth_basic_user_file /home/paul/atwell/htpasswd;
index index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;}}

What am I doing wrong?

Disable Cache for static files (css, js, etc.) in the DEV system (1 reply)

$
0
0
Hi,

I would like to disable the cache function for static files into the dev system, but I can't find the right parameter or systemfile to change this.

I've try to change in the /etc/nginx/site-available/DOMAIN/
location ~\.(css|js)$ {
expires epoch;
}
nothing happens
location ~\.(css|js)$ {
expires 0;
}
nothing happens
location ~\.(css|js)$ {
expires off;
}
notting happens
location ~\.(css|js)$ {
add_header Cache-Control "max-age=0, public, must-revalidate, proxy-revalidate";
}
nothing happens
location ~\.(css|js)$ {
add_header Cache-Control "no-cache";
}
etc ....

please can someone of you help me? currently I am just freak out ...

Direct Upload without temp files (no replies)

$
0
0
Hello,

we use Nginx as reverse proxy for several websites. One of your sites provides uploads of huge files (> 2gb).
The nginx Server is hosted on a small linux box (without much resources (ram/hd).

Nginx caches large files in temp path and sends the request after the whole file is processed by nginx to the backend.
We are looking for a direct streaming solution.

Additionally we use some kind of XSRF Token on backend which is only valid for several seconds, but in case of huge uploads the token gets invalid because the request on backend starts after the whole file is uploaded to nginx server.

I have read about several solutions (upload module, lua resty upload, client_body_in_file_only) which all deal with storing the files on nginx box and striping the body for backend with additional headers to process the temp file directly on the backend.

We don't want to modify our applications to handle uploads in that way (it feels a little bit hackish).

Are there any working solution to send the body in junks to the backend server?

Kind Regards
Offtopic

Problem ModSecurity & OpenSSL (no replies)

$
0
0
Hello,

I installed NGINX with:
--add-module=../mod_security/nginx/modsecurity --with-http_ssl_module --with-openssl=/xxx

If i start the services i got a lot of this:
2014/04/13 21:24:35 [alert] 11297#0: *1741 zero size buf in writer t:0 r:0 f:0 0000000000000000 0000000000000000-0000000000000000 0000000000000000 0-0 while sending to client, client: 33.80.250.218, server: xx, request: "GET /wp-content/plugins/scbb.css?ver=0.5.7_482014120156 HTTP/1.1", upstream: "https://xx/wp-content/plugins/scbb.css?ver=0.5.7_482014120156", host: "xx", referrer: "xx"

and:
2014/04/13 21:25:33 [alert] 12065#0: worker process 12068 exited on signal 11
2014/04/13 21:25:33 [alert] 12065#0: worker process 12069 exited on signal 11
2014/04/13 21:25:33 [alert] 12065#0: worker process 12070 exited on signal 11
2014/04/13 21:25:38 [alert] 12065#0: worker process 12071 exited on signal 11
2014/04/13 21:26:07 [alert] 12065#0: worker process 12072 exited on signal 9

Using NGINX 1.4.7 + Latest Mod_Security + Latest OpenSSL
I got this with SSL Wordpress. (HTML = Random worker process alert only)

Because it's a clean installation, i think it is not a big problem.

How to clear the cache in a proper way - do I need to reload the server for this? (no replies)

$
0
0
Hello,
I have scripts created that clear the cache by deleting all the cache folder or specific files from the cache according to keys. Do I need to reload the server with "killall -HUP nginx" after deleting files?
As I understand, If I just delete the file, the cached object key is still stored on memory. Is it cleared automatically? Does reloading help to clear it? I don't want server RAM to be wasted for storing non-existing objects.

I got concerned about this after I noticed the following records in error.log file:

2014/04/13 03:43:47 [crit] 23617#0: unlink() "/usr/local/nginx/cache/site1/2d/ca/ef5a2b4a35d954542d4c6eeb99dfca2d" failed (2: No such file or directory)

A lot of such errors were generated. I guess NGINX cannot find the deleted file? The generation of these messages ended after I run the "killall -HUP nginx" command on the server.
I have a server running multiple sites, but each site has its own cache folder and own cache key zone name.

Could you please answer my questions?

Thank you,

Rokas Zakarevicius, Ph.D.

Nginx 1.4.7 + Prestashop + URL rewriting (no replies)

$
0
0
Hi,

I’m using Nginx 1.4.7 on a Debian server.

I’m trying to use url rewriting with Prestashop (shop CMS). They give a conf file for url rewriting. This conf works well with Nginx 1.2.1 but absolutly doesn't work for images with 1.4.7.

location / {
index /index.php;

rewrite ^/([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$1$2.jpg last;
rewrite ^/([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$1$2$3.jpg last;
rewrite ^/([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$3/$1$2$3$4.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$3/$4/$1$2$3$4$5.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9.jpg last;
rewrite ^/c/([0-9]+)(-[_a-zA-Z0-9-]*)/[_a-zA-Z0-9-]*.jpg$ /img/c/$1$2.jpg last;
rewrite ^/c/([a-zA-Z-]+)/[a-zA-Z0-9-]+.jpg$ /img/c/$1.jpg last;
rewrite ^/([0-9]+)(-[_a-zA-Z0-9-]*)/[_a-zA-Z0-9-]*.jpg$ /img/c/$1$2.jpg last;

try_files $uri $uri/ /index.php?$args;
}

Does anyone know if there is changes between Nginx 1.2.1 and 1.4.7 ? Or maybe its a bug ?

Compare $http_referer and $host (no replies)

$
0
0
Hi,

Is it possible to compare $http_referer and $host variables with regexp like this?

if ( $http_referer ~ $host ) {
set $match 1;
}

I want to check if http_referer is equal to host in order to send pages from cache. I have one default_server config because I'm using wildcard as domain name so any domain name pointed to IP will show default page.

I've tried with ~* ^http://$host but it does not work probably because of $ sign in host variable.

Any advice is welcome.

Issues setting up Fastcgi for Perl (3 replies)

$
0
0
So recently I decided to switch from Apache 2.4 to Nginx, largely for better performance. I also wanted to get a new site going that would be written in Perl and served by FastCGI, as well as support my current wordpress sites.

The PHP portion of the configuration was easy as pie. I installed and configured php-fpm, and after a few tweaks everything was working as before (except slightly faster). So far so good.

Perl hasn't been quite as easy to set up. I feel a bit like someone falling down a tree and getting his nuts smashed by every branch on the way down, if you'll pardon the expression. However I'm now at a point where I've tried every fix for my remaining problem and the test page is still not up and running.

My setup is as follows: Amazon Linux AMI running in EC2, have installed nginx, perl and spawn-fcgi from the repository, have compiled and installed fcgiwrap.

Nginx, php-fpm, and spawn-fcgi/fcgiwrap are all set to run as www-data:www-data and execute as daemons.

For the defined web roots I have made sure that all the directories all the way to root are o+x, and the web, log and other directories (e.g. /var/lib/nginx) themselves are owned by www-data:www-data. All FCGI requests are communicated through a socket file, both for php-fpm and fcgiwrap, although different sockets are used. Oddly enough the php-fpm socket appears to be owned by root:root while the fcgiwrap socket is owned by www-data:www-data. Things have been configured largely as per the instructions here:

http://www.howtoforge.com/serving-cgi-scripts-with-nginx-on-centos-6.0-p2

And still all I get when I try to hit my test page is "403: forbidden". It states just that in smallcaps -- View Source shows nothing else than that one line -- so this is not the standard NGINX error page. I have tried tailing the error log while making the request, but nothing gets added (like there's effectively no error), nor can I find anything relevant in /var/log/messages.

I'm getting to my wits' end trying to diagnose this. As I mentioned I've tried a lot of the fixes one can find with a google search, but nothing has resolved the issue so far. Can anyone suggest something I might be missing?

Nginx as a forward proxy - denying contents by MIME type (no replies)

$
0
0
Hi,

I'd like to use nginx 1.2.1 as a filtering & caching proxy on my Raspberry Pi. Filtering domain names and URLs is easy but I could not find a way to filter contents by their MIME type.

To be a little bit more specific, I want to deny clients to download or watch multimedia contents (MIME types: audio/*, video/*). In order to achieve this, I created a mapping like this:

map $sent_http_content_type $is_forbidden {
default 0;
"~audio\/.*" 1;
"~video\/.*" 1;
}

I tried various methods to send a 403 forbidden response to the client in case a forbidden content is being downloaded, without any luck.

1. Tried specifying in an if block - which would never work as it turned out that if statements are expanded before the response is received so the variable $sent_http_content_type will always be empty and thus the mapping returns 0:

location / {
if ($is_forbidden = 1) {
return 403;
}

2. Tried using the add_header directive to append a header to the response:

location / {
add_header X-Forbidden $is_forbidden;
}

This works like a charm, I can see on the client that my custom header contains 0 or 1 according to the content's MIME type, yet I can't evaluate the header's value server-side for the same reason as above.

3. Tried creating a second proxy instance to check the value of X-Forbidden there but that also didn't work as expected, I couldn't push the forwarded requests to the destination servers without causing 400 errors - this is likely to be a configration issue although I don't think this solution would be the most appropriate one.

I'm aware that there are several alternatives (eg. Squid 3 has the exact capability I need) but other solutions would need more resources and therefore would run much slower on a small embedded device like my Raspberry Pi.

Please assist me, I'm completely stuck with this.

Thanks,
Gergely
Viewing all 2931 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>