0

This has been a struggle for the past two days. I've done a lot of searching, couldn't find anything to fix it.

Here's my nginx.conf:

 user www-data;
    worker_processes 4;
    pid /run/nginx.pid;

    events {
            worker_connections 768;
            # multi_accept on;
    }

    http {

            ##
            # Basic Settings
            ##

            sendfile on;
            tcp_nopush on;
            tcp_nodelay on;
            keepalive_timeout 65;
            types_hash_max_size 2048;
            # server_tokens off;

            # server_names_hash_bucket_size 64;
            # server_name_in_redirect off;

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

            ##
            # Logging Settings
            ##

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

            ##
            # Gzip Settings
            ##

            gzip on;
            gzip_disable "msie6";

            # gzip_vary on;
            # gzip_proxied any;
            # gzip_comp_level 6;
            # gzip_buffers 16 8k;
            # gzip_http_version 1.1;
            # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

            ##
            # Virtual Host Configs
            ##

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

Here's my default file in /etc/nginx/sites-enabled:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html/;
    index index.php index.html index.htm;

    server_name [redacted server IP];

    location / {
        try_files $uri $uri/ =404;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ /admin/ {
        auth_basic "Restricted";
        auth_basic_user_file /etc/nginx/.htpasswd;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.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;
    }


}

The page is stored in /usr/share/nginx/html/admin/index.php -- it prompts for the username/password (which works fine), but as soon as I login, it downloads instead of showing the php webpage.

If I put the 'location ~ /admin/' block after the 'location ~ .php$' block, it works, but doesn't authenticate.

There are scripts I wrote in another folder that work fine when called 'in the folder /usr/share/nginx/html/api/XXX.php' (which inserts stuff into the MySQL database), but it just returns JSON stuff.

You are awesome for helping me out, I really do appreciate it.

2
  • uncomment that 1 line and it will work. Commented Dec 3, 2014 at 20:47
  • @r3wt I tried that, no luck. I'll change the code in the post to show Commented Dec 3, 2014 at 21:10

2 Answers 2

4

You are commenting the php5-fpm sock, delete the comment from the following line:

fastcgi_pass unix:/var/run/php5-fpm.sock;

Change your admin path configuration to use the php5-fpm sock:

location ~ /admin/.+\.php$ {
  auth_basic "Restricted";
  auth_basic_user_file /etc/nginx/.htpasswd;
  fastcgi_split_path_info ^(.+\.php)(/.+)$;
  fastcgi_pass unix:/var/run/php5-fpm.sock;
  include fastcgi_params;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_index index.php;
 }

OR move your admin configuration path AFTER the ~\.php$ {} rule (both worked for me)

make sure the php5-fpm service is running and you've restarted nginx

sudo service php5-fpm restart && sudo service nginx restart
Sign up to request clarification or add additional context in comments.

9 Comments

No luck, I un-commented that line, saved, and ran your commands (getting as a response: php5-fpm stop/waiting php5-fpm start/running, process 20506 * Restarting nginx nginx ) What do you mean by 'make sure the file exist in this path'?
are you getting any errors in the access.log/error.log? tail /var/log/nginx/error.log || tail /var/log/nginx/access.log
what about the access.log? is there anything there?
the nginx configuration is configured good so far, how are you accessing your local enviorment? try to set up a virtualhost edit your hosts file sudo nano /etc/hosts add a line to the hosts file 127.0.0.1 local.test.com. change the server_name [redacted server IP]; to server_name local.test.com;; restart your nginx service sudo service nginx restart. and try to access your website using this url: local.test.com. let me know if it works.
also make sure your php file name is index.php
|
0

Try adding the default PHP block into your conf.

location ~ \.php$ {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
            return 404;
    }

   fastcgi_pass 127.0.0.1:9000;
   fastcgi_index index.php;
   include fastcgi_params;
}

Without this block the expected behavior is to show the file. You need to have PHP FPM running and configured to make this work. Instructions are available at http://wiki.nginx.org/PHPFcgiExample

4 Comments

I tried this (commented out the location block and inserted this one into the /sites-enabled/default file), but no luck. I'm 94% sure my PHP FPM is running and configured -- do you know how I would check this? EDIT: also, this renders the /api/XXX.php files useless, so I had to revert to what I had originally
To check if php-fpm is running service php5-fpm status. If php-fpm is configured to listen to a port, you can check the corrent port and ip with netstat -ntaup |grep php. If this does not return anything, php-fpm is listening to unix socket. To check this run ls /var/run/php*sock.
Service is running: 'php5-fpm start/running, process 20506'. Running "sudo netstat -ntaup | grep php" doesn't return anything (this is a remote server). Doing the 'ls /var/run/php*sock' gets the return: "/var/run/php5-fpm.sock"
Also, I tried following the nginx.org link you sent me to the letter, but still no luck. And I'm trying every time incognito mode, to avoid a cookie problem

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.