0

I have Nginx server. Currently, I have two projects:

  1. www.xyz.com -> /var/www/project/public; (This is working)
  2. www.xyz.com/blog -> /var/www/blog; (Error: This webpage has a redirect loop)

How can I point both to the above folders. I tried the following code.

server {
 listen 80;
 server_name xyz.com;
 rewrite ^/(.*)/$ /$1 permanent;
 return 301 $scheme://www.xyz.com$request_uri;
}

server {
    listen   80 default_server;
    server_name www.xyz.com;
    rewrite ^/(.*)/$ /$1 permanent;
    root /var/www/project/public;
    index index.php index.html index.htm;

   location / {
            try_files $uri $uri/ /index.php?$query_string;
    }


    location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
            expires 365d;
    }

    location ~*  \.(pdf)$ {
            expires 30d;
    }

    location ~ \.(?:css|htc|js|js2|js3|js4)$ {
            gzip_vary on;
    }

    # pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock

    location ~ \.php$ {
            try_files $uri /index.php =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
   }

   location /blog {
        root /var/www/blog;
   }

   location ~ /blog/.+\.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME /var/www/blog$fastcgi_script_name;
    }
}
3
  • What works? What doesn't?
    – gxx
    Jan 23, 2016 at 12:59
  • @gf_ www.xyz.com is working and www.xyz.com/blog says "This webpage has a redirect loop".
    – Chopra
    Jan 23, 2016 at 13:01
  • Alright..so please put this into your question, so it makes sense and people are actually able to help you.
    – gxx
    Jan 23, 2016 at 13:04

1 Answer 1

0

I think you should use a plus php section in location /blog:

location /blog {
  alias /var/www/blog;
  location ~ \.php$ {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
  }
}

And you don't need location ~ /blog/.+\.php$.

8
  • I am getting 404 - Not found error. 2016/01/23 18:44:52 [error] 12447#0: *3 open() "/var/www/blog/blog" failed (2: No such file or directory), client: 27.51.217.2, server: www$
    – Chopra
    Jan 23, 2016 at 13:15
  • Removed /blog from /var/www/blog and now, it gives 502 Bad Gateway
    – Chopra
    Jan 23, 2016 at 13:18
  • Sorry, it's my fault - I don't check your /blog section. You should use alias instead of root. I think now will fine :)
    – uzsolt
    Jan 23, 2016 at 15:06
  • Now it's saying, No input file specified.
    – Chopra
    Jan 23, 2016 at 15:42
  • 2016/01/23 21:13:17 [error] 20425#0: *214 FastCGI sent in stderr: "Unable to open primary script: /var/www/blogx.php (No such file or directory)" while reading response header from upstream,$
    – Chopra
    Jan 23, 2016 at 15:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .