1

I have a site that uses subdirectories and currently only works when the trailing slash is added to the URL ("http://www.domain.com/dir/"). When there is no trailing slash, I get "unable to connect at server domain.com:8080" (8080 is the listening port Nginx is set up for).

I've tried adding the rewrite suggested here (and here), but it results in the "cannot connect" error for the entire virtual host.

Is there another way to add the trailing slash that I could try? Or, is there a way I can configure it to see the URL as a directory (and thus, look for the index file), regardless of the presence of the trailing slash?

Edit

Nginx.conf:

user www-data;
worker_processes  4;

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

events {
    worker_connections  1024;
    # multi_accept on;
}

http {
    include       /etc/nginx/mime.types;

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

    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay        on;

    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    map $scheme $fastcgi_https { ## Detect when HTTPS is used
        default off;
        https on;
    }

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

Server block:

server {
        listen 8080;
        server_name domain.com www.domain.com;
        include www.inc;

        root /var/vhosts/domain/current/frontend/;
        include php.inc;
}

Php.inc:

index index.php;

location ~ \.php$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    #fastcgi_param  ENVIRONMENT production;
    fastcgi_param HTTPS $fastcgi_https;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    #fastcgi_intercept_errors on;
    fastcgi_connect_timeout 10;
    fastcgi_send_timeout 15;
    fastcgi_read_timeout 120;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
    include fastcgi_params;
}

www.inc:

if ($host ~ ^([^\.]+\.com)) {
    rewrite ^/(.*)$ http://www.$host/$1 permanent;
}
Community
  • 1
  • 1
Shauna
  • 9,495
  • 2
  • 37
  • 54
  • Please add server { } config where rewrite doesn't work. – Timofey Stolbov May 03 '11 at 20:18
  • Added, as well as the includes. I just thought - is the www rewrite the problem? If so, I could modify it to add the slash there, but would it work for URLs that are already including www? Is there a better way to modify it? I need the www check. – Shauna May 03 '11 at 20:31

1 Answers1

1

Change server {} block to

server {
    listen 8080;
    port_in_redirect off;
    server_name www.domain.com domain.com; #Order matters!
    include www.inc;

    root /var/vhosts/domain/current/frontend/;
    include php.inc;
}
Timofey Stolbov
  • 4,501
  • 3
  • 40
  • 45
  • Still nothing. I tried just editing the rewrite line itself, since the server is hosting a number of different sites (so I have to be careful). I also tried replacing the include call in sites-available itself with your suggestion and still nothing. Would your suggestion even work if the URL is something like "http://www.domain.com/dir"? It would seem to me that it'd only work for "domain.com/dir". – Shauna May 04 '11 at 19:02
  • @Shauna, I finally got it. Your server listens on non-standard port so redirection must include $server_port. See updated version. – Timofey Stolbov May 04 '11 at 19:05
  • Nope. "Cannot establish a connection at domain.com:8080". With the "default" (ie - the one I use for the other sites) setup, it already goes to port 8080, FWIW. – Shauna May 04 '11 at 19:12
  • In theory. Unfortunately, in practice, that's not happening. =/ – Shauna May 04 '11 at 19:20
  • @Shauna, what exactly request you're making? Are you really have only one location {} block? – Timofey Stolbov May 04 '11 at 19:25
  • What I've provided is exactly what I'm using (I'm open to optimization suggestions, I inherited this stuff). As for the calls, now that this site/app are officially live, here's the live, working URL (and what I want it to go to): http://www.duethealth.com/ohiohealth/ ... Here's a few of the permutations that I've tried: http://www.duethealth.com/ohiohealth http://duethealth.com/ohiohealth duethealth.com/ohiohealth – Shauna May 04 '11 at 20:08
  • @Shauna, your server doesn't allow connect to port 8080. It's normal as you're using Varnish. See updated answer. – Timofey Stolbov May 04 '11 at 20:48
  • Your suggestion for the www.inc doesn't work (results in "server isn't redirecting properly" error), but using the default www.inc, switching the server_name list, and adding `port_in_redirect off` did work! Thank you so much! And thank you for all the time you've spent helping. :D – Shauna May 04 '11 at 20:55
  • @Shauna, don't forget to look http://wiki.nginx.org/HttpCoreModule#server_name_in_redirect directive for understand why server_name order is important. – Timofey Stolbov May 04 '11 at 21:02