I have PHP FPM + Nginx setup. One of my PHP applications sets an invalid content length header, so I'm trying to ignore it using fastcgi_hide_header, but it doesn't work. It works for headers other than Content-Length, so I assume there is a problem with that in particular.
What is the correct way of doing this? I cannot modify the PHP application to fix the source of the problem.
server {
listen 8000 default_server;
root /var/www;
index index.php index.html index.htm;
rewrite_log on;
# Make site accessible from http://localhost/
server_name localhost;
location / {
try_files $uri $uri/ /index.php;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
fastcgi_hide_header X-Fake-Header;
fastcgi_hide_header Content-Length;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Output if I remove the code in PHP that sets the headers (this is the desired output):
< HTTP/1.1 200 OK
* Server nginx/1.4.1 (Ubuntu) is not blacklisted
< Server: nginx/1.4.1 (Ubuntu)
< Date: Thu, 13 Feb 2014 01:58:07 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
< X-Powered-By: PHP/5.5.3-1ubuntu2.1
If I leave the code in, but use the above nginx config, I get this:
< HTTP/1.1 200 OK
* Server nginx/1.4.1 (Ubuntu) is not blacklisted
< Server: nginx/1.4.1 (Ubuntu)
< Date: Thu, 13 Feb 2014 01:59:09 GMT
< Content-Type: text/html
< Content-Length: 6
< Connection: keep-alive
< X-Powered-By: PHP/5.5.3-1ubuntu2.1
I ended up having to user the HttpHeadersMore module in Nginx (if you're on Ubuntu, this is included with nginx-extras but not nginx-full).
With the module installed, I just added the following to my Nginx configuration:
more_clear_headers Content-Length;
This worked as expected.
Related
Receive 404 error while calling URL - http://10.240.0.133/swagger. Below is the snippet of nginx.conf file, I need to append index.html at end of the URI, so I placed a rewrite rule.
server {
listen 80;
listen [::]:80;
server_name localhost;
server_name 10.240.0.133;
server_name 127.0.0.1;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
access_log /var/log/nginx/resources-reverse-access.log;
error_log /var/log/nginx/resources-reverse-error.log;
location /swagger {
rewrite ^/swagger/index.html break;
proxy_pass http://52.177.131.103:8082/;
}
}
When I visited the URL - curl -v http://10.240.0.133/swagger
404 is thrown:-
* Trying 10.240.0.133...
* TCP_NODELAY set
* Connected to 10.240.0.133 (10.240.0.133) port 80 (#0)
> GET /swagger HTTP/1.1
> Host: 10.240.0.133
> User-Agent: curl/7.55.1
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Server: nginx/1.14.0 (Ubuntu)
< Date: Wed, 18 Mar 2020 14:41:50 GMT
< Content-Length: 0
< Connection: keep-alive
<
* Connection #0 to host 10.240.0.133 left intact
I believe your rewrite rule is incorrect. It should look more like this.
location /swagger {
rewrite ^\/swagger\/?.*?$ /swagger/index.html break;
proxy_pass http://52.177.131.103:8082/;
}
but I believe this still not correct since you have not a set a root directive for this server.
I'm trying to configure Location directive on my nginx web-server (Ubuntu).
I can have access to:
http://127.0.0.1/app1/
BUT when I'm trying to get access whitout slash in the end like:
http://127.0.0.1/app1
I get an error 301 HTTP1.1/Moved permanently
I have following nginx config:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Looks like everything is OK.
And following default.conf:
server {
listen 80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location /app1/ {
root /var/www/html/;
index index.html;
try_files $uri $uri/ /app1/index.html;
}
}
Curl output
http://127.0.0.1/app1/
root#ubuntu-test:/etc/nginx/sites-available# curl 127.0.0.1/app1/ -Iv
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> HEAD /app1/ HTTP/1.1
> Host: 127.0.0.1
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Server: nginx/1.14.0 (Ubuntu)
Server: nginx/1.14.0 (Ubuntu)
< Date: Thu, 20 Feb 2020 09:14:12 GMT
Date: Thu, 20 Feb 2020 09:14:12 GMT
< Content-Type: text/html
Content-Type: text/html
< Content-Length: 5
Content-Length: 5
< Last-Modified: Tue, 18 Feb 2020 10:49:53 GMT
Last-Modified: Tue, 18 Feb 2020 10:49:53 GMT
< Connection: keep-alive
Connection: keep-alive
< ETag: "5e4bc151-5"
ETag: "5e4bc151-5"
< Accept-Ranges: bytes
Accept-Ranges: bytes
<
* Connection #0 to host 127.0.0.1 left intact
http://127.0.0.1/app1
root#ubuntu-test:/etc/nginx/sites-available# curl 127.0.0.1/app1 -Iv
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> HEAD /app1 HTTP/1.1
> Host: 127.0.0.1
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
HTTP/1.1 301 Moved Permanently
< Server: nginx/1.14.0 (Ubuntu)
Server: nginx/1.14.0 (Ubuntu)
< Date: Thu, 20 Feb 2020 09:19:31 GMT
Date: Thu, 20 Feb 2020 09:19:31 GMT
< Content-Type: text/html
Content-Type: text/html
< Content-Length: 194
Content-Length: 194
< Location: http://127.0.0.1/app1/
Location: http://127.0.0.1/app1/
< Connection: keep-alive
Connection: keep-alive
Why does it happen?
Nginx selects the location / block to process the URI /app1 as no other locations are a better match. See how Nginx processes a request.
The $uri/ term of the try_files statement informs Nginx to append a / to any URI that matches a directory. The directory /var/www/html/app1 matches this requirement, so a 301 redirection is generated to append a / to the URI. See this document for details.
In addition, the default behaviour for a URI which ends with a / and points to a directory is to search that directory for a file that matches the index directive. See this document for details.
It is possible to deviate from this default behaviour, but you will need to make a number of changes to your configuration. The location /app1/ needs to lose the trailing / if you want it to match /app1. Your try_files directives need to lose the $uri/ term, if you want to avoid the 301 redirect. You will also lose default index processing, so the index directive will be useless.
It's quite strange to see this issue as title describes. Here are the details:
1) Created a A-record with "a1". Means subdomain is: a1.example.com,
2) created directory by "sudo mkdir -p /var/www/a1.example.com/html/"
3) Created server block by "sudo nano /etc/nginx/sites-available/a1.example.com"
4) Inside the server block, core parts:
server {
listen 80;
listen [::]:80;
root /var/www/a1.example.com/html;
index index.html index.htm;
server_name a1.example.com
location / {
try_files $uri $uri/ =404;
}
}
5) created the link: sudo ln -s /etc/nginx/sites-available/a1.example.com /etc/nginx/sites-enabled/
6) sudo nano /etc/nginx/nginx.conf, uncomment: server_names_hash_bucket_size 64;
Make sure: include /etc/nginx/sites-enabled/*; is there
7) restarted nginx: sudo service nginx restart
Tricky part: By doing so, the first subdomain, a1.example.com works fine.
But when I tried to create the second subdomain, b2.example.com following the exact steps above, b2.example.com simply does't work: it will redirect to the main page, but the URL remains as b2.example.com
what can be wrong?
thank you very much!
config for a1.example.com
server {
listen 80;
listen [::]:80;
root /var/www/a1.example.com/html;
index index.php index.html index.htm;
server_name a1.example.com;
rewrite ^/archives/(\d+)$ http://a1.example.com/?p=$1 permanent;
location / {
try_files $uri $uri/ /index.php?q=$uri&args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
}
config for b2.example.com
server {
listen 80;
listen [::]:80;
root /var/www/b2.example.com/html;
index index.php index.html index.htm;
server_name b2.example.com;
rewrite ^/archives/(\d+)$ http://b2.example.com/?p=$1 permanent;
location / {
try_files $uri $uri/ /index.php?q=$uri&args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
}
a portion of log from access.log
[my IP address] - - [04/Feb/2016:00:11:05 -0500] "GET / HTTP/1.1" 200 147 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:43.0) Gecko/20100101 Firefox/43.0"
[my IP address] - - [04/Feb/2016:00:11:05 -0500] "GET /favicon.ico HTTP/1.1" 404 142 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:43.0) Gecko/20100101 Firefox/43.0"
[my IP address] - - [04/Feb/2016:00:11:08 -0500] "GET / HTTP/1.1" 200 17176 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:43.0) Gecko/20100101 Firefox/43.0"
[my IP address] - - [04/Feb/2016:00:11:09 -0500] "GET /wp-includes/css/dashicons.min.css?ver=4.4.1 HTTP/1.1" 304 0 "http://b2.example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:43.0) Gecko/20100101 Firefox/43.0"
[my IP address] - - [04/Feb/2016:00:11:09 -0500] "GET /wp-content/uploads/wp-less/themename/css/main-a800d37d44.css?ver=2.3.3 HTTP/1.1" 301 184 "http://b2.example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:43.0) Gecko/20100101 Firefox/43.0"
I did notice "304" and "301", http status. But still have no clue
curl -I b2.example.com:
HTTP/1.1 200 OK
Server: nginx/1.8.1
Date: Thu, 04 Feb 2016 05:54:22 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.5.9-1ubuntu4.14
Set-Cookie: PHPSESSID=4r143k0o1m1l62p3g4vt1jrav1; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Link: <http://www.example.com/wp-json/>; rel="https://api.w.org/"
Link: <http://www.example.com/>; rel=shortlink
curl -I a1.example.com
HTTP/1.1 200 OK
Server: nginx/1.8.1
Date: Thu, 04 Feb 2016 05:54:32 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.5.9-1ubuntu4.14
Link: <http://a1.example.com/wp-json/>; rel="https://api.w.org/"
Ugh, been trying to get NGINX/php-fpm to play nicely with SF 1.4 for a few days now, and can't quite seem to nail down the proper config. I followed the nginx symfony guide as well as this SO post, but neither helped, and I suspect it may be because they were being configured against older versions of NGINX (I am working with 1.6.2).
Here is my config:
server {
listen 51000;
server_name example.mpurcell.dev.example.com;
access_log /tmp/access.log;
error_log /tmp/error.log notice;
root /home/mpurcell/projects/j1n/app/example/current/code/web/;
index index.php;
location ~ ^/(app|app_dev)(/|$) {
rewrite ^(.*)$ $1.php last;
}
location ~ ^/(app|app_dev).php(/|$) {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_param SERVICE_ENV 'dev';
fastcgi_param HTTPS off;
# http://wiki.nginx.org/Symfony
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass unix:/var/run/php-fpm.sock;
}
}
And the various responses:
$ -> curl -v 10.0.0.7:51000
# Expected
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.6.2
< Date: Wed, 01 Oct 2014 23:34:10 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
< Location: /app
$ -> curl -v 10.0.0.7:51000/app.php
# Expected
< HTTP/1.1 200 OK
< Server: nginx/1.6.2
< Date: Wed, 01 Oct 2014 23:37:48 GMT
< Content-Type: text/html; charset=utf-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Cache-Control: private
$ -> curl -v 10.0.0.7:51000/app
# Not expected, the script executes but SF throws a 404 with the following error
# Empty module and/or action after parsing the URL "/app" (/).
< HTTP/1.1 404 Not Found
< Server: nginx/1.6.2
< Date: Wed, 01 Oct 2014 23:39:09 GMT
< Content-Type: text/html; charset=utf-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Cache-Control: private
And it sure looks like the rewrite rule from the vhost config is working:
2014/10/01 23:40:30 [notice] 9668#0: *13 "^(.*)$" matches "/app", client: 10.0.0.3, server: example.mpurcell.dev.example.com, request: "GET /app HTTP/1.1", host: "dev-a-2:51000"
2014/10/01 23:40:30 [notice] 9668#0: *13 rewritten data: "/app.php", args: "", client: 10.0.0.3, server: example.mpurcell.dev.example.com, request: "GET /app HTTP/1.1", host: "dev-a-2:51000"
And for the sake of completness, the cgi.fix_pathinfo is default (=1), and I don't really want to set this to 0.
Also, I should note that relative_url_root for the app controller is set to empty string, as it is located in the root web directory.
Stack:
nginx 1.6.2
php-fpm 5.4.33
php 5.4.33
I think your issue is that you've not told nginx where Symfony is. I've put an example of nginx configuration that I'm using currently that works.
server {
listen 80;
server_name example.com;
chunked_transfer_encoding on;
proxy_buffering off;
charset utf-8;
root /home/mpurcell/projects/j1n/app/example/current/code/web;
access_log /tmp/access_log;
error_log /tmp/error_log;
location /sf/ {
expires max;
root /home/mpurcell/projects/j1n/app/example/current/code/lib/vendor/symfony/data/web/;
}
location ~ ^/(index|frontend_dev|backend_dev)\.php($|/) {
set $script $uri;
set $path_info "";
if ($uri ~ "^(.+\.php)(/.*)") {
set $script $1;
set $path_info $2;
}
include fastcgi_params;
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_FILENAME $document_root$script;
fastcgi_param SCRIPT_NAME $script;
fastcgi_param HTTPS off;
fastcgi_pass 127.0.0.1:9000;
fastcgi_keep_conn on;
fastcgi_intercept_errors on;
}
location / {
if (-f $request_filename) {
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
break;
}
if ($request_filename ~ ".(js|htc|ico|gif|jpg|png|css)$") {
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
break;
}
index index.php;
try_files $uri /index.php?$args;
}
}
For you problem I think the key is this:
location /sf/ {
expires max;
root /home/mpurcell/projects/j1n/app/example/current/code/lib/vendor/symfony/data/web/;
}
So I finally got Symfony and php-fpm to play nicely with each other, and one big piece of that puzzle was to swap out apache for nginx. IMO, the rewrite syntax for nginx > apache. So here is an example of my current app server config:
location #rewrite {
rewrite ^/(.*)$ /index.php/$1 last;
}
location /admin {
rewrite ^/admin/(.*)$ /admin/index.php/$1 last;
}
location /app {
rewrite ^/app/(.*)$ /index.php/$1 last;
}
location ~ index\.php {
...
}
I did have to create sub dirs for each controller in the web dir, like this:
/web
index.php
app.php
admin.php
/web
/app/index.php
/admin/index.php
I've had this config in prod now for about 2 months with 0 issues, so hopefully this helps other old school symfonians as well.
This is the config I'm using for symfony 1.4
location ~ \.php($|/) {
fastcgi_pass php56:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
}
Here's the relevant nginx configuration fragment:
location ^~ /static/ {
expires max;
rewrite ^/static/[^/]+/(.*)$ /$1 last;
}
After reloading nginx there's no sign of the Expires header for some reason:
$ curl -i http://monda.hu/static/1/blog/wp-content/icomoon/style.css
HTTP/1.1 200 OK
Server: nginx/1.4.1 (Ubuntu)
Date: Sat, 21 Dec 2013 22:55:46 GMT
Content-Type: text/css
Content-Length: 1783
Last-Modified: Tue, 16 Jul 2013 21:03:00 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "51e5b504-6f7"
Accept-Ranges: bytes
(The idea here is to increment the numeric version value in the URL whenever the related resource changes. Please do not tell me that expires max is a bad practice. I think it's a matter of taste and priorities.)
Other than the above there are the following location blocks present in the relevant server section:
location ~ \.php$ { ... }
location / { ... }
location ~ /\. { ... }
location ^~ /mydbadmin/ { ... }
location #php { ... } # this one is not even referenced