【问题标题】:nginx codeigniter 502 bad gatewaynginx codeigniter 502 网关错误
【发布时间】:2023-04-05 00:54:01
【问题描述】:

nginx的配置如下:

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;
        }
}

请给我一些建议,谢谢~

【问题讨论】:

  • 您从哪里获得应用程序根目录上的502 bad gateway
  • 是的,在应用程序根目录
  • @RahilWazir 在应用程序根目录
  • @RahilWazir Centos5.7 X86_64
  • 对于初学者,请确保 php 服务正在运行 ps -ef | grep phpsudo netstat -ntlp | grep 9000

标签: codeigniter nginx


【解决方案1】:

我终于自己做对了。

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;
    }
}

【讨论】:

    【解决方案2】:

    请将下面一行添加到 Nginx 配置文件 /etc/nginx/nginx.conf 中

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

    reference

    【讨论】:

      【解决方案3】:

      我在 Codeigniter + nginx 上也遇到了这个错误,但我已经通过更改代码解决了这个问题。 问题在于会话。在会话中,我保存了 stdClass 对象。当我更改值或从会话中检索值时,它给了我 502 bad gateway。所以我将会话值更改为 Associative Array 然后我的问题就解决了。我认为会话存储值超过这就是服务器给出错误 502 bad gateway 的原因。

      【讨论】:

        【解决方案4】:
        • 您在location / 中没有根(这可能没问题)

        • 1234563
        • 您缺少一些建议的参数

        这是我运行并使用 CI (CentOS 6) 的 nginx 配置。它从 URL 中删除 index.php。它也是 SSL,但如果你不需要,你可以把那个垃圾拿出来。它至少应该为您指明正确的方向。

        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;
        

        【讨论】:

          猜你喜欢
          • 2011-05-14
          • 2019-06-05
          • 2015-08-10
          • 2021-11-19
          • 2012-09-25
          • 2014-12-07
          • 2020-09-29
          • 2012-07-16
          • 2015-04-30
          相关资源
          最近更新 更多