【问题标题】:PHP files can't be executed after enabling HTTPS on Nginx/php-fpm在 Nginx/php-fpm 上启用 HTTPS 后 PHP 文件无法执行
【发布时间】:2015-04-20 07:05:35
【问题描述】:

我在 Nginx/php-fpm 服务器上启用了 HTTPS,服务器仍然运行良好但我无法访问 php 文件,它将被下载而不是运行它,如果我禁用 HTTPS 阻止,那么 php 文件可以可以访问。我是 HTTPS 新手,这是我第一次拥有 SSL 证书,所以我想设置到我的服务器。 有什么想法吗?

我的/etc/nginx/nginx.conf:

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

worker_rlimit_nofile 10240;
events {
        use epoll;
        worker_connections 10240;
        # multi_accept on;
}
timer_resolution 500ms;

http {

        ##
        # Basic Settings
        ##

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

        client_header_buffer_size 64;
        client_max_body_size 6m;

        server_names_hash_bucket_size 64;
        server_name_in_redirect off;

        include /etc/nginx/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"';
        ##
        # Logging Settings
        ##

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

    output_buffers 1 32k;
    postpone_output 1460;

    open_file_cache max=1000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;


    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 32k;
    fastcgi_buffers 4 32k;
    fastcgi_busy_buffers_size 32k;
    fastcgi_temp_file_write_size 32k;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    gzip_proxied expired no-cache no-store private auth;
    gzip_min_length 0;
    gzip_comp_level 2;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;


    proxy_cache_path  /var/lib/nginx/cache  levels=1:2   keys_zone=staticfilecache:80m inactive=1d  max_size=2500m;
    proxy_temp_path /var/lib/nginx/proxy;
    proxy_connect_timeout 300;
    proxy_read_timeout 120;
    proxy_send_timeout 120;
    proxy_buffer_size  16k;
    proxy_buffers      4 16k;
    ##
    # Virtual Host Configs
    ##

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

这是我的 /etc/nginx/sites-available/default:

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

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

        # Make site accessible from http://localhost/
        server_name repodev.com;
        return 301 https://$host$request_uri; #rewrite http to https

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

        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 /usr/share/nginx/html;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        location ~ \.php$ {
                try_files $uri =404;
        #       fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #
        #       # With php5-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;

        #       # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
        location ~ /\.ht {
                deny all;
        }
}

cgi.fix_pathinfo = 0已经设置在/etc/php5/fpm/php.ini上,这是我的SSL配置文件,/etc /nginx/conf.d/ssl.conf:

server {
       listen 443 default_server ssl;
       server_name repodev.com;

       ssl_certificate /etc/ssl/unified.crt;
       ssl_certificate_key /etc/ssl/my-private-decrypted.key;

       ssl_session_cache shared:SSL:10m;
       ssl_session_timeout 10m;
       keepalive_timeout 70;

       ssl_prefer_server_ciphers on;
       ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA RC4 !aNU$
       ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

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

}

【问题讨论】:

  • 请不要在标题中添加[solved],而是将最正确的答案标记为已接受。

标签: ssl nginx https php


【解决方案1】:

请理解,SSL 服务器与非 SSL 服务器是完全独立的对象。这意味着,您必须将location ~ \.php$ { ... } 块添加到 SSL 版本,如果您希望它使用 PHP,它不是继承自非 SSL 服务器。

编辑

这当然也适用于您的error_page 和其他所有内容。

【讨论】:

  • 哦,感谢您的快速回复。所以我需要在我的 /etc/nginx/conf.d/ssl.conf 中设置 location ~ \.php$ { ... } 块,不是吗?
  • 至少 - 我会走另一条路,从非 SSL conf 的副本开始,然后添加 SSL 的东西。
  • 哦,非常感谢,我已经将 location ~\.php${ .. } 设置到我的 ssl.conf 中,现在我的服务器可以使用 https 访问 php 文件,提前谢谢先生..
  • @kaichi 您应该将此答案标记为正确,因为它解决了您的问题
猜你喜欢
  • 1970-01-01
  • 2017-02-19
  • 1970-01-01
  • 2021-11-24
  • 2019-08-09
  • 2017-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多