Nginx和PHP-FPM在vhost中不起作用

时间:2014-10-12 18:31:00

标签: nginx php gentoo

我正在尝试使用虚拟主机将项目移动到Nginx + PHP-FPM。但是当我尝试加载网站时,我收到一个名为'download'的文件,其中包含index.php的内容。

nginx.conf的内容:

user nginx nginx;

worker_processes 4;
worker_rlimit_nofile 64000;

error_log /var/log/nginx/error_log debug;

events {
        worker_connections 16000;
        multi_accept on;
        use epoll;
}

http {
        log_format main
                '$remote_addr - $remote_user [$time_local] '
                '"$request" $status $bytes_sent '
                '"$http_referer" "$http_user_agent" '
                '"$gzip_ratio"';

        access_log on;

        disable_symlinks if_not_owner;
        ignore_invalid_headers on;
        server_tokens off;

        keepalive_timeout 20;
        client_header_timeout 20;
        client_body_timeout 20;
        reset_timedout_connection on;
        send_timeout 20;

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;

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

        gzip on;
        gzip_vary on;
        gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js image/x-icon image/bmp;

        server {
                location ~ \.php$ {
                        try_files $uri =404;
                        include /etc/nginx/fastcgi.conf;
                        fastcgi_pass unix:/run/php-fpm.socket;
                        fastcgi_index index.php;
                }
        }
        include /etc/nginx/sites-enabled/*;
}

vhost文件的内容:

 server {
        listen 127.0.0.1:80;
        server_name yps.dev;

        access_log /var/log/nginx/yps.access_log main;
        error_log /var/log/nginx/yps.error_log debug;

        root /home/bobbles/projects/yps_upstream/www/public;
        index index.cgi index.htm index.html index.php;

        location ~ \.php$ {
                try_files $uri =404;
                include /etc/nginx/fastcgi.conf;
                fastcgi_pass unix:/run/php-fpm.socket;
                #fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
        }
}

包含的fastcgi参数:

# cat /etc/nginx/fastcgi.conf 

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

PHP-FPM池:

[www]
listen =/run/php-fpm.socket
listen.owner = nginx
listen.mode = 0666
user = nobody
group = nobody
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
php_admin_value[error_log] = /var/log/fpm-php.www.log
php_admin_flag[log_errors] = on

套接字:

# ls -al /run/php-fpm.socket 
srw-rw-rw- 1 nginx nginx 0 Oct 12 22:12 /run/php-fpm.socket

在访问日志和错误日志中,除错误日志中的超时外没有输出:

==> /var/log/nginx/error_log <==
2014/10/12 22:15:39 [info] 3317#0: *19 client closed connection while waiting for request, client: 127.0.0.1, server: 0.0.0.0:80
2014/10/12 22:15:39 [info] 3317#0: *20 client closed connection while waiting for request, client: 127.0.0.1, server: 0.0.0.0:80

访问日志是静默的。

我做错了什么?永远不会创建/var/log/fpm-php.www.log,所以我认为这意味着请求永远不会进入php-fpm,但是我的nginx配置有什么问题?

编辑:

当我尝试从目录访问静态文件时会发生这种情况:

==> /var/log/nginx/localhost.error_log <==
2014/10/12 21:16:04 [error] 3021#0: *3 openat() "/usr/share/nginx/html/email.html" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /email.html HTTP/1.1", host: "yps.dev"

==> /var/log/nginx/localhost.access_log <==
127.0.0.1 - - [12/Oct/2014:21:16:04 +0200] "GET /email.html HTTP/1.1" 404 410 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36" "3.10"

==> /var/log/nginx/yps.error_log <==
2014/10/12 21:16:23 [info] 3021#0: *4 client closed connection while waiting for request, client: 127.0.0.1, server: 0.0.0.0:80

显然请求同时被传递给yps虚拟主机,而不是。

1 个答案:

答案 0 :(得分:0)

这是您的vhost文件的更新版本,可能有效:

server {
        listen 80;
        server_name yps.dev;

        access_log /var/log/nginx/yps.access_log main;
        error_log /var/log/nginx/yps.error_log debug;

        root /home/bobbles/projects/yps_upstream/www/public;
        index index.php index.htm index.html;
        location / {
            try_files $uri $uri/ @handler; 
        }

        location @handler { 
            rewrite / /index.php;
        }

        location ~ \.php$ {
                if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/run/php-fpm.socket;
                #fastcgi_pass 127.0.0.1:9000;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                fastcgi_index index.php;
                include /etc/nginx/fastcgi.conf;

        }
}