nginx codeigniter 502 bad gateway

2019-04-13 04:41发布

The config of nginx is as follows:

server {
        listen       80;
        server_name  www.example.com;

        root   /home/wwwroot/example.com;
        index index.php  index.html index.htm;

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

        location ~ \.php($|/) {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            fastcgi_param   PATH_INFO $fastcgi_path_info;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

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

        location ~ /\.ht {
                deny  all;
        }
}

please give me some advice, thank you~

4条回答
看我几分像从前
2楼-- · 2019-04-13 05:19

I finally make it right myself.

server {
    listen       80;
    server_name  example.com;

    root   /home/wwwroot/example.com;
    index index.php  index.html index.htm;

    location / {
    root /home/wwwroot/example.com;
            index  index.php index.html index.htm;
        if (!-e $request_filename) {
                rewrite ^/(.*)$ /index.php/$1 last;
                break;
        }
    }
    location ~ \.php($|/) {
        fastcgi_pass  unix:/tmp/php-cgi.sock;
        fastcgi_index  index.php;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_param   PATH_INFO $fastcgi_path_info;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    location ~ /\.ht {
            deny  all;
    }
}
查看更多
对你真心纯属浪费
3楼-- · 2019-04-13 05:20

I was also getting this error on Codeigniter + nginx but i have solved it by changing my code. The problem is with the session. In the Session i was saving the stdClass object. When i change the value or retrieve the value from session it gives me 502 bad gateway. So i change the session value to Associative Array and then my problem is solved. I think session storage value get exceed this is why the server give the error 502 bad gateway.

查看更多
我只想做你的唯一
4楼-- · 2019-04-13 05:35

please add the following line to Nginx configuration file /etc/nginx/nginx.conf

http {
    ...
    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;
    ...
}

reference

查看更多
Juvenile、少年°
5楼-- · 2019-04-13 05:38
  • You don't have a root in location / (this might be OK)

  • You haven't stated whether or not you are trying to remove index.php from the url (if you are trying to visit a URL without index.php and without the rewrite, this may lead to the 502)

  • You are missing some suggested params

Here is an nginx config I have running and working with CI (CentOS 6). It removes index.php from the URL. It's also SSL but you can just take that junk out if you don't need. It should at least point you in the right direction.

server {
    listen       80;
    server_name  _;
    access_log   /var/www/https/logs/access.log;
    error_log    /var/www/https/logs/error.log;
    return       301 https://www.example.com$request_uri;
}

server {
    listen 443 default_server ssl;
    server_name *.example.com;
    ssl on;
    ssl_certificate /var/www/ssl/wildcard.example.com.chained.crt;
    ssl_certificate_key /var/www/ssl/wildcard.example.com.key;
    ssl_verify_depth 3;
    access_log /var/www/https/logs/ssl/access.log;
    error_log /var/www/https/logs/ssl/error.log;

    #http://mailman.nginx.org/pipermail/nginx-announce/2013/000125.html
    if ($request_uri ~ " ") {
        return 444;
    }

    location /  {
        root   /var/www/https/;

        # file doesn't exist, let CI handle it
        if (!-f $request_filename) {
                rewrite ^(.*) /index.php?$1 last;
        }
    }

    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
        root              /var/www/https/;
        access_log        on;
        expires           30d;
    }

    location ~ \.php$ {
        include fastcgi.conf;#/etc/nginx/fastcgi.conf
        fastcgi_param SCRIPT_FILENAME /var/www/https$fastcgi_script_name;
    }
}


/etc/nginx/conf.d/fastcgi.conf:

fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
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 PATH_INFO $fastcgi_path_info;
fastcgi_param SERVER_PROTOCOL $server_protocol;

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;

fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
查看更多
登录 后发表回答