T086ѧϰ�� | վ��ѧԺ | �����ĵ� | ���� | Ъ���� | IP��ַ��ѯ | �������� | ���ջ��� | �����ֵ� | ���ù��� | ������Ϣ��ѯ

����:������ȷ����Nginx+PHP

�� �𶡱ʼ� ����ʱ�䣺2013-10-23 | ���壺�� �� С��
[����]�Ժܶ��˶��ԣ�����Nginx+PHP��������������һƪ�̳̣�Ȼ�󿽱�ճ��������ȥ�ƺ�Ҳûʲô���⣬��ϧʵ���������Ϻܶ����ϱ�������ʧ�ޣ�©���ٳ����������Ҳ������⣬һζ�Ŀ���ճ����������һ����Ϊ�˸������ۡ�����...

�Ժܶ��˶��ԣ�����Nginx+PHP��������������һƪ�̳̣�Ȼ�󿽱�ճ��������ȥ�ƺ�Ҳûʲô���⣬��ϧʵ���������Ϻܶ����ϱ�������ʧ�ޣ�©���ٳ����������Ҳ������⣬һζ�Ŀ���ճ����������һ����Ϊ�˸������ۡ�

����������PHPʵ����һ��ǰ�˿�����������ֱ�׵�˵����ͳһ���ڣ���PHP���󶼷��͵�ͬһ���ļ��ϣ�Ȼ���ڴ��ļ���ͨ��������REQUEST_URI��ʵ��·�ɡ�

��ʱ�ܶ��̳̻��̴�����������Nginx+PHP��

server {
    listen 80;
    server_name foo.com;

    root /path;

    location / {
        index index.html index.htm index.php;

        if (!-e $request_filename) {
            rewrite . /index.php last;
        }
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /path$fastcgi_script_name;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
    }
}

�������кܶ����󣬻���˵�����ǻ�ζ���ĵط������ҿ����ܷ��ּ�����

��

�����б�Ҫ���˽�һ��Nginx�����ļ���ָ���ļ̳й�ϵ��Nginx�����ļ���Ϊ�ö��飬�����Ĵ��⵽�������ǡ�http������server������location���ȵȣ�ȱʡ�ļ̳й�ϵ�Ǵ��⵽�ڣ�Ҳ����˵�ڲ������Զ���ȡ��������ֵ��Ϊȱʡֵ�������⣬�����ο�����

���UNDERSTANDING THE NGINX CONFIGURATION INHERITANCE MODEL

��

�������ȴӡ�index��ָ�����ְɣ������������������ڡ�location���ж����ģ�

location / {
    index index.html index.htm index.php;
}

һ��δ����Ҫ�����µġ�location������Ȼ�������ظ������ġ�index��ָ�������Ϊ������location����ƽ���Ĺ�ϵ�������ڼ̳У���ʱӦ���ڡ�server���ﶨ�塸index���������̳й�ϵ����index��ָ�������еġ�location���ж�����Ч��

���Nginx Pitfalls

��

������������if��ָ�˵���Ǵ�������������Nginxָ������Ϊ����

if (!-e $request_filename) {
    rewrite . /index.php last;
}

�ܶ���ϲ���á�if��ָ����һϵ�еļ��飬������ʵ�����ǡ�try_files��ָ����ְ����

try_files $uri $uri/ /index.php;

�������⣬��ѧ����������Ϊ��if��ָ�����ں˼���ָ�����ʵ��������rewriteģ����һ���֣�����Nginx����ʵ����������ʽ�ģ����ǹ���ʽ�ģ����Ե����ͷ�rewriteģ����ָ������ʱ���������ܻ�������Ը��

�ο���IfIsEvil and How nginx ��location if�� works

��

���濴����fastcgi_params�������ļ���

include fastcgi_params;

Nginx������fastcgi�����ļ����ֱ��ǡ�fastcgi_params���͡�fastcgi.conf��������û��̫���IJ��죬Ψһ�������Ǻ��߱�ǰ�߶���һ�С�SCRIPT_FILENAME���Ķ��壺

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

ע�⣺$document_root �� $fastcgi_script_name ֮��û�� /��

ԭ��Nginxֻ�С�fastcgi_params�����������ֺܶ����ڶ��塸SCRIPT_FILENAME��ʱʹ����Ӳ�����ķ�ʽ������Ϊ�˹淶�÷��������ˡ�fastcgi.conf����

���������Ļ��Ͳ���һ�����ʣ�Ϊʲôһ��Ҫ����һ���µ������ļ����������޸ľɵ������ļ���������Ϊ��fastcgi_param��ָ���������͵ģ�����ָͨ����ͬ���ǣ��ڲ��滻���㣻����ָͨ�ͬ���ǣ�����ͬ������ʹ�õ�ʱ�����������������滻�����仰˵��������ͬ���������Ρ�SCRIPT_FILENAME������ô���Ƕ��ᱻ���͵����ˣ������ܻᵼ��һЩDZ�ڵ����⣬Ϊ�˱���������������������һ���µ������ļ���

���FASTCGI_PARAMS VERSUS FASTCGI.CONF �C NGINX CONFIG HISTORY

��

���⣬���ǻ���Ҫ����һ����ȫ���⣺��PHP������cgi.fix_pathinfo���������£�PHP���ܻ��Ѵ������ļ����͵���PHP�ļ�������������Nginx��PHP��װ��ͬһ̨�������ϵĻ�����ô���򵥵Ľ����������á�try_files��ָ����һ�ι��ˣ�

try_files $uri =404;

�ο���Nginx�ļ����ʹ�������©��

��

����ǰ���ķ���������һ�ݸ������İ汾���Dz��DZȿ�ʼ�İ汾��ˬ�˺ࣺܶ

server {
    listen 80;
    server_name foo.com;

    root /path;
    index index.html index.htm index.php;

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

    location ~ \.php$ {
        try_files $uri =404;

        include fastcgi.conf;
        fastcgi_pass 127.0.0.1:9000;
    }
}

ʵ���ϻ���һЩ覴ã���Ҫ�ǡ�try_files���͡�fastcgi_split_path_info���������ݣ���Ȼ�ܹ��������������Ƚϳ�ª�������Ͳ���˵�ˣ�����Ȥ�Ŀ��Բο�����������

���䣺��Ϊ��location���Ѿ������޶������ԡ�fastcgi_index����ʵҲû�б�Ҫ��

��

ϣ�������Ժ���Ҫ�ڿ���ճ���ˣ�����ʵ�ڸIJ��ˣ���ô���뿽��ճ�����ġ�

ԭ�ģ�http://huoding.com/2013/10/23/290

  • ת����ע����Դ��ITѧϰ�� ��ַ��http://www.t086.com/ �����������Ƽ�������
  • �ر������� ��վ�������ر�������ֹת�ص�ר�������������¿�������ת�أ���������ע��������ԭʼ���ߡ����°�Ȩ������ԭʼ�������С����ڱ���վת�����µĸ��˺���վ�����DZ�ʾ������л�⡣������վת�ص������а�Ȩ��������ϵ���ǣ����ǻᾡ�����Ը�����
  • ��
  • ��
  • ��
  • ��
  • ��
  • ��
  • ��
  • ��
  • ��
������ѯ Health Check Gulangyu
© 2017 T086ѧϰ�� - T086.com(ԭitlearner.com)
RunTime:7.75ms QueryTime:7