Welcome! Log In Create A New Profile

Advanced

reverse proxy and location directive

Posted by jemari 
reverse proxy and location directive
May 14, 2009 11:29AM
Hello,

I am a newbie when it comes to nginx, and a bit more familiar with apache.

I have the following: default nginx installation on ubuntu 8.04, and apache2 on the backend.

Here is nginx.conf
[code]
user www-data www-data;
worker_processes 2;

error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

access_log /var/log/nginx/access.log;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;

gzip on;

include /etc/nginx/sites-enabled/*;
}
[/code]

I also have the following two symlinks under [b]sites-enabled[/b]
[code]
server {
listen 80;
server_name www.domain1.com;

# Main location
location / {
proxy_pass http://127.0.0.1:81/;
proxy_redirect off;
}

# Static files location
location ~* \.(js|css|ico|jpg|jpeg|gif)$ {
root /home/myName/domain1/www;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}

[/code]

[code]
server {
listen 80;
server_name www.domain2.com;

# Main location
location / {
proxy_pass http://127.0.0.1:81/;
proxy_redirect off;
}

# Static files location
location ~* \.(js|css|ico|jpg|jpeg|gif)$ {
root /home/myName/domain2/www;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}

[/code]

But, I get the following error, [b]in the error log of domain1[/b] when I try to access www.domain2.com/some.js (a similar call works perfectly for domain1, and also for domain2 if I use the port 81 (apache2 listens there) directly.
[code]
"/home/myName/domain2/www/some.js" failed (2: No such file or directory), client: [ my ip ], server: domain1.com, URL: "/some.js", host: "www.domain2.com"
[/code]

I don't get it why it serves php files correctly, but not .js files. Can anyone help?
Re: reverse proxy and location directive
May 14, 2009 04:59PM
Try adding "Host" header to each server config.

[code]
server {
listen 80;
server_name www.domain1.com;

# Main location
location / {
proxy_pass http://127.0.0.1:81/;
proxy_redirect off;
proxy_set_header Host $host;
}

# Static files location
location ~* \.(js|css|ico|jpg|jpeg|gif)$ {
root /home/myName/domain1/www;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
[/code]


See if that works.

Are you using Apache only for php?

--
Jim Ohlstein
Re: reverse proxy and location directive
May 14, 2009 06:02PM
Thanks Jim.

Currently, yes, I am using apache mainly for php. I also have few html files, which I might want nginx to serve directly.

I already have the host and other proxy parameters working well. (I just removed them, to reduce my post size).

But, that shouldn't have an effect anyway because any .js, .css and other 'static' files, ought to be served by nginx directly due to the following [i][b]location[/b][/i] directive
[code]
# Static files location
location ~* \.(js|css|ico|jpg|jpeg|gif)$ {
root /home/myName/domain1/www;
}
[/code]
Which is the same in both servers (of course, with differing roots).

The problem is, it works perfectly for the first one. For the second one, it somehow tries to find the file in the root of domain1 ... and that's why it doesn't find it. If it was apache's problem, then the 404 page should also have been apache's, not nginx'.

Since, the location directive is valid only within the [url=http://wiki.nginx.org/NginxHttpCoreModule#location]context of server[/url], I am very confused and clueless why it serves (tries to serve) domain2.com from domain1.com's root.

Is it not possible to have more than one proxy domains, as in my case?

jemari
Re: reverse proxy and location directive
May 14, 2009 06:11PM
I want to add,

If I remove the first one from sites-enabled domain2 works ok, i.e. I see both php and js files as expected. But, when I enable domain1 again, domain2 starts returning 404 for js.

Thanks in advance for any help.
Re: reverse proxy and location directive
May 14, 2009 06:23PM
That is very odd. Are the two domains on the same IP?

--
Jim Ohlstein
Re: reverse proxy and location directive
May 14, 2009 06:35PM
YES.

All on the same vps. For the sake of completeness here are my configs again:

nginx.conf (nothing edited)
[code]
user www-data www-data;
worker_processes 2;

error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

access_log /var/log/nginx/access.log;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
#keepalive_timeout 0;
keepalive_timeout 65;


gzip on;

include /etc/nginx/sites-enabled/*;

}

[/code]

domain1 under sites-enabled (created with ln -s)
Real domain name and real path changed here.
[code]
server {
listen www.domain1.example.com:80;
server_name domain1.example.com www.domain1.example.com *.domain1.example.com;

access_log /home/me/domain1/logs/nginx.access.log;
error_log /home/me/domain1/logs/nginx.error.log warn;

# Main location
location / {
proxy_pass http://127.0.0.1:8080/;
proxy_redirect off;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

client_max_body_size 10m;
client_body_buffer_size 128k;

proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;

proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}

# Static files location
# location ~* \.(js|css|ico|jpg|jpeg|gif|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf)$ {
location ~* \.(js|css|ico|jpg|jpeg|gif)$ {
root /home/me/domain1/public;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
[/code]

server2
[code]
server {
listen 80;
server_name www.domain2.example.org test.domain2.example.org;

access_log /home/me/domain2/logs/nginx.access.log;
error_log /home/me/domain2/logs/nginx.error.log warn;

# Main location
location / {
proxy_pass http://127.0.0.1:8080/;
proxy_redirect off;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

client_max_body_size 10m;
client_body_buffer_size 128k;

proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;

proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}

# Static files location
# location ~* \.(js|css|ico|jpg|jpeg|gif|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf)$ {
location ~* \.(js|css|ico|jpg|jpeg|gif)$ {
root /home/me/domain2/public;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}

[/code]
Re: reverse proxy and location directive
May 14, 2009 06:58PM
Your listen directives appear to be different. That may or may not matter depending on the version you have (see http://nginx.net/CHANGES bugfix for 0.7.51). Otherwise I'm stumped.

--
Jim Ohlstein
Re: reverse proxy and location directive
May 14, 2009 07:10PM
You got it, Jim!

nginx -v returned [i]nginx/0.5.33[/i]

I changed it back to 80 and it works fine now. The irony is though, I added that while trying to solve the same error.

Anyways, thank a lot! I appreciate it a lot.
Re: reverse proxy and location directive
May 14, 2009 07:34PM
Great!

Once you are more comfortable you should think about having nginx serve all static content including html. Then you can strip down the number of modules that Apache loads so it consumes less memory, or (better yet in my opinion) use nginx's built in fastcgi support and eliminate Apache altogether.

--
Jim Ohlstein
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 69
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready