nginx codeigniter 502 bad gateway - codeigniter

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~

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

please add the following line to Nginx configuration file /etc/nginx/nginx.conf
http {
...
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
...
}
reference

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.

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;

Related

Nginx, urls with trailing slash and query string break path parsing for query_string and request_uri

Im working on a Laravel app hosted via Nginx that is replacing an old system that ran on Apache 2.2.
The new system must support all urls currently supported by the old system.
Most urls are like this, and work fine:
https://somesite.com/some/path?someProp=1
However, some urls have a trailing backslash AND a query string, like the below:
https://somesite.com/some/path/?someProp=1
(While such links are surely bad practice, the app must support them for backwards compatibility with other related systems).
In my app, I've found that when the urls have a trailing backslash AND a query string, the query string and path get clobbered somewhere along the way, for example, given this code:
public function boot(): void
{
$uri = $_SERVER['REQUEST_URI'];
$query = $_SERVER['QUERY_STRING'];
}
For urls without a trailing slash I get:
$uri === https://somesite.com/some/path?someProp=1
$query === someProp=1
But for urls with a trailing slash I get:
$uri === https://somesite.com/some/path//1
$query === ''
How can I configure Nginx to set the $request_uri and $query_string values properly when a url may contain a trailing backslash AND a query string?
Nginx server config:
server {
listen 80;
listen 443 ssl;
server_name somesite.com;
ssl_certificate /etc/ssl/wildcard/tls.crt;
ssl_certificate_key /etc/ssl/wildcard/tls.key;
root /home/httpd/htdocs/public;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include includes/php-fpm.conf;
fastcgi_pass unix:/socket/monolith.php-fpm.sock;
fastcgi_param PHP_VALUE "auto_prepend_file=None
auto_append_file=None";
}
}
php-fpm.conf
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param HTTP_PROXY "";
fastcgi_index index.php;
include fastcgi_params;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
#fixes timeouts
fastcgi_read_timeout 600;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#astcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_URL $uri;
fastcgi_param SCRIPT_URI $scheme://$http_host$uri;
fastcgi_params:
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 SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;
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;

official laravel nginx config doesn't work for me

On my local machine doesn't work nginx config from official laravel documentation
server {
listen 80;
server_name example.com;
root /example.com/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
But it worked with changes in a few lines
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
changed to
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
and everything works fine. Can anyone explane me the difference in that piece of code and is it unsecure ?
Only reasonable diff I see inclusion of files: snippets/fastcgi-php.conf and fastcgi_params
You issue could be fixed simply by adding:
fastcgi_param SCRIPT_FILENAME $request_filename;
to /etc/nginx/fastcgi_params file.
In old versions of nginx that line existed.
But from 1.10.x it was removed and become reason of blank screen.

laravel not working in nginx as alias

i have a nginx webserver and i want to run multiple laravel projects on it
first project is working fine but second project only opens home and home page address is
example.com/secondproject/
but when i want to open
example.com/secondproject/foo
nginx would skip /secondproject alias and looks for requested route in first project (root of server in nginx config)
this is my config
server {
listen 80;
server_name example.com;
rewrite_log on;
location / {
root /path/to/first-project/public;
index index.php index.html index.htm;
try_files $uri $uri/ $uri/index.php /index.php;
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
}
}
location /secondproject {
alias /path/to/second-project/public;
index index.php;
location /secondproject {
try_files $uri $uri/ $uri/index.php /index.php;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
}
}
}
and when i replace
root /path/to/second-project/public;
instead of
alias /path/to/second-project/public;
nginx shows 403!
try like this:
server {
listen 80;
server_name example.com www.example.com;
location / {
root /path/to/first-project/public;
index index.php index.html index.htm;
try_files $uri $uri/ $uri/index.php /index.php;
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
}
}
# ------- second
location /secondproject {
root /path/to/second-project/public;
index index.php index.html index.htm;
try_files $uri $uri/ $uri/index.php /index.php;
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
}
}
}

Codeigniter nginx 404

I'm using Ubuntu on webmin nginx.
I am not sure how many times this question has been answered before, but every answer that I look at gives a different approach to solving this problem of which none of them worked. My /etc/nginx/sites-available/example.com looks like this...
Codeigniter nginx giving continuous 404 error.
What can I do?
My config file:
server {
server_name example.com www.example.com;
listen 00.000.00.00:80;
root /home/example/public_html;
index index.html index.htm index.php;
access_log /var/log/virtualmin/example.com_access_log;
error_log /var/log/virtualmin/example.com_error_log;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
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_FILENAME /home/example/public_html$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT /home/example/public_html;
fastcgi_param SERVER_PROTOCOL $server_protocol;
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;
fastcgi_param HTTPS $https;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/php-nginx/14449619992080.sock/socket;
}
listen 00.00.00.00:443 default ssl;
ssl_certificate /home/example/ssl.cert;
ssl_certificate_key /home/example/ssl.key;
}

nginx webpy fastcgi caching can not work

nginx.conf file content as follow:
http {
include mime.types;
default_type application/octet-stream;
# configure cache log
log_format cache '$remote_addr - $host [$time_local] '
'"$request" $status $upstream_cache_status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
fastcgi_cache_path /data0/nginx-cache levels=1:2
keys_zone=nginx_fastcgi_cache:1m
inactive=1d;
fastcgi_temp_path /data0/nginx-cache/temp;
server {
listen 8080;
server_name outofmemory.cn localhost;
access_log /data0/nginx-1.2.6/logs/cache.log cache;
#charset koi8-r;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache nginx_fastcgi_cache;
fastcgi_cache_min_uses 1;
fastcgi_ignore_headers Cache-Control Expires;
fastcgi_cache_use_stale error timeout invalid_header http_500;
#add_header X-Cache cached;
fastcgi_cache_valid 60m;
location / {
root /www/outofmemory.cn;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_pass 127.0.0.1:9002;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache nginx_fastcgi_cache;
fastcgi_cache_valid 60m;
}
}
}
any help would be appreciated.
thanks.
I also having the same problem. As yukaizhao mentioned in his post, need to added below to ignore expires header or else fastcgi_cache won't work.
fastcgi_ignore_headers "Cache-Control" "Expires" "Set-Cookie";
Thanks yukaizhao!
I have resolved this question.
I wrote an artile to explain how to configure nginx + webpy + fastcgi cache.
http://outofmemory.cn/code-snippet/2154/nginx-webpy-fastcgi-cache-configuration-explain-in-detail
thanks.

Resources