centos配置php-fpm和nginx的通信


以下是nginx的默认配置文件:
 server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    } 

一个location匹配一个(一种)url。

匹配到对应的url就转到对应的location中,然后进行处理请求。

默认的root是/usr/share/nginx/html,也就是会转到对应的目录下进行处理请求。

但是匹配到有php的文件就没有办法进行解析。

这里用到了php-fpm 也就是fastcgi在php下的一个类库。

当匹配到有.php结尾的请求的时候,就将该请求转到fastcgi进行处理。

关于server_name 还纠结了好久,当我在同一操作系统下有两个用户a和b,两者都有各自的root目录进行访问,并且同时都配置了各自的server。但是nginx的配置文件是在全局其作用的。那么如果在局域网中c访问这个服务器的时候,那么到底访问哪个目录呢?

后来,哥哥告诉我了。

一般在上线的网站中配置nginx的时候,server_name是域名,根据不同的域名,nginx自然是知道怎么去转的。

如果是在局域网中用ip进行访问,如果存在着两个server,那么就配置一个当用ip进行访问的时候,默认跳转到一个目录下就可以了。

自己在线下进行折腾的时候,就配置一个人server就可以了。

默认就会请求这个目录,并用fastccgi进行转发。

在配置nginx的时候发现了一个问题,访问的root目录下,.html结尾的静态文件是可以正常被访问的,.php结尾的就不行。

后来才发现,在进行配置的时候,将root变成了局部变量,导致匹配php文件的时候,找不到root。自然就找不到要访问的文件了。

最后的配置如下:

\

相关内容

    暂无相关文章