Rumah  >  Artikel  >  pembangunan bahagian belakang  >  macos - mac 配置的PHP Nginx 环境 访问php文件出现下载而不是执行php文件

macos - mac 配置的PHP Nginx 环境 访问php文件出现下载而不是执行php文件

WBOY
WBOYasal
2016-12-01 01:27:392113semak imbas

<code>vim /usr/local/etc/nginx/sites-available/default
</code>
<code>server {
    listen       80;
    server_name  localhost;
    root         /var/www/;

    access_log  /usr/local/var/logs/nginx/default.access.log  main;

    location / {
        index  index.html index.htm index.php;
        autoindex   on;
        include     /usr/local/etc/nginx/conf.d/php-fpm;
    }

    location = /info {
        allow   127.0.0.1;
        deny    all;
        rewrite (.*) /.info.php;
    }

    error_page  404     /404.html;
    error_page  403     /403.html;
}
</code>
<code>/usr/local/etc/nginx/nginx.conf
</code>
<code>worker_processes  1;

error_log   /usr/local/var/logs/nginx/error.log debug;


pid        /usr/local/var/run/nginx.pid;


events {
    worker_connections  256;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    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  /usr/local/var/logs/access.log  main;

    sendfile        on;
    keepalive_timeout  65;
    port_in_redirect off;

    include /usr/local/etc/nginx/sites-enabled/*;
}

</code>

/usr/local/etc/nginx/sites-available/default 这个文件配置的是80端口,为啥80端口不能访问, 但是8080 可以访问

80 端口访问都是出现404 错误找不到

回复内容:

<code>vim /usr/local/etc/nginx/sites-available/default
</code>
<code>server {
    listen       80;
    server_name  localhost;
    root         /var/www/;

    access_log  /usr/local/var/logs/nginx/default.access.log  main;

    location / {
        index  index.html index.htm index.php;
        autoindex   on;
        include     /usr/local/etc/nginx/conf.d/php-fpm;
    }

    location = /info {
        allow   127.0.0.1;
        deny    all;
        rewrite (.*) /.info.php;
    }

    error_page  404     /404.html;
    error_page  403     /403.html;
}
</code>
<code>/usr/local/etc/nginx/nginx.conf
</code>
<code>worker_processes  1;

error_log   /usr/local/var/logs/nginx/error.log debug;


pid        /usr/local/var/run/nginx.pid;


events {
    worker_connections  256;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    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  /usr/local/var/logs/access.log  main;

    sendfile        on;
    keepalive_timeout  65;
    port_in_redirect off;

    include /usr/local/etc/nginx/sites-enabled/*;
}

</code>

/usr/local/etc/nginx/sites-available/default 这个文件配置的是80端口,为啥80端口不能访问, 但是8080 可以访问

80 端口访问都是出现404 错误找不到

首先你需要一个 php-fpm 来跑 php,然后还要把它配置到正确的位置

参考我的 conf:

<code>server {
  listen 80;

  server_name xxxxxx;

  index index.php;

  root /usr/www/web-sites/xxxxxx;

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

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

  location ~ ^/.+\.php(/|$) {
    fastcgi_pass php_fpm:9000;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }

  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ {
    expires 1d;
  }
}</code>

有问题可以搜索关键字:nginx 连接 php-fpm
我提供的这种是 http 方式,你可以换用效率更高的 socket 方式,关键字:socket nginx php-fpm

  • 404错误你可以看看error.log日志

  • 你有没有发现php-fpm和nginx没有结合?

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn