25

What's wrong with my server definition? If I try to access to "www.testing.com" I get a binary to download instead of the index.php, instead if I try to access to "testing.com" I get the index.php.

I already tried to set servername to:

servername testing.com;
servername testing.com www.testing.com;
servername testing.com www.testing.com *.testing.com;

Same behavior: I can't get index.php with "www.testing.com", just with "testing.com". (off course testing.com is not mine is just for example).

    user              nginx;
    worker_processes  4;
    error_log         /var/log/nginx/error.log warn;
    pid               /var/run/nginx.pid;

    events {
         worker_connections  1024;
    }


    http {
         include      /etc/nginx/mime.types;
         default_type  text/plain;

         log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                            '$status $body_bytes_sent "$http_referer" '
                            '"$http_user_agent" "$http_x_forwarded_for"';

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

         fastcgi_intercept_errors    on;
         sendfile                    on;
         keepalive_timeout           65;
         gzip                        on;
         index                       index.php index.html index.htm;

         server {
              listen 80;
              server_name www.testing.com;
              root /home/vhosts/testing;

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

        location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
                  expires max;
                  add_header Pragma public;
                  add_header Cache-Control "public, must-revalidate, proxy-revalidate";
              }

        location ~* \.php$ {
                 try_files $uri =404;
                 include fastcgi.conf;
                 fastcgi_pass  127.0.0.1:9000;
              }
         }
    }

5 Answers 5

24

first you need to check your php-fpm settings (maybe you using socket connection instead of port in your php-fpm configuration) and add index by default in your location "/"

location / {
    index index.php index.html index.htm;
    try_files $uri $uri/ =404;
}
2
  • 2
    Did not work. browser still tries to download the file (did nginx restart)
    – vsync
    Apr 29, 2018 at 17:58
  • I was missing this one in try_files $uri/
    – arsenik
    Dec 30, 2020 at 16:08
10

Add fastcgi_index index.php; in location ~* \.php$:

location ~* \.php$ {
    try_files $uri =404;
    include fastcgi.conf;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index  index.php;
}
0
2

This one worked for me:

location = / {
    index index.php index.html index.htm;
    try_files $uri /index.html;
}

The whole location config with proxy is:

location = / {
    index index.php index.html index.htm;
    try_files $uri /index.html;
    proxy_pass http://localhost:8081;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}
2

Check so fpm is running on 127.0.0.1:9000

location ~ \.php$ {
     try_files $uri =404;
     include fastcgi.conf;
     fastcgi_pass  127.0.0.1:9000;
}

also log error and check:

error_log  /var/log/nginx/error.log  debug;

For a sample configuration check: https://github.com/rtCamp/easyengine/blob/master/conf/nginx/singlesite/basic.conf

-3

You can have more than one servername line, it will set up a VHOST on all of them.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.