I'm struggling trying to load static contents on my page using NGINX, and below is my current configuration. My goal is to make my page load faster that's why I want to load the static content through NGINX, but that I notice the loading time of my page is the same.
Configuration
File Path : etc/nginx/default
server {
listen 80 default_server;
server_name default;
root /home/forge/site/public;
location / {
proxy_pass http://43.35.49.160/;
try_files $uri $uri/ /index.php?$query_string;
}
# Media: images, icons, video, audio, HTC
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
# CSS and Javascript
location ~* \.(?:css|js)$ {
expires 1y;
access_log off;
add_header Cache-Control "public";
}
# cache.appcache, your document html and data
location ~* \.(?:manifest|appcache|html?|xml|json)$ {
expires -1;
access_log logs/static.log; # I don't usually include a static log
}
# Feed
location ~* \.(?:rss|atom)$ {
expires 1h;
add_header Cache-Control "public";
}
#include folder/file-name.conf;
}
cURL TEST
curl -X GET -I http://45.33.69.160/
HTTP/1.1 200 OK
Server: nginx/1.6.3
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Cache-Control: no-cache
Date: Sat, 09 May 2015 15:09:02 GMT
Set-Cookie: XSRF-TOKEN=eyJpdiI6IkVNZ205RjhuNWI2aUtEOUxRbjJSdUE9PSIsInZhbHVlIjoiN2RtSlJVZUZ6ZFpWNUNETElZOTNuK1ZzQW5Lcmw5UTVKVmo1REFRT2w0cVJoS3IxUitaOWNKYkh5NERrQXZ5VXpjbmNkSlZ0NTFwXC9XNGVxQ0xVYXlBPT0iLCJtYWMiOiI4Yjk4NjI1YjdjMDgxMzJiOTk4MzIxNmUzOTMxM2JiNDY2OTc4ZTZkNzI4Y2IxNjZlYmViNTAyYmZhY2ZlYjYzIn0%3D; expires=Sat, 09-May-2015 17:09:02 GMT; Max-Age=7200; path=/
Set-Cookie: laravel_session=eyJpdiI6Ik5zKzRcLzBEWE5mUjVhUlwvVE9GeWk3Zz09IiwidmFsdWUiOiJCT0pqaWFFQlhCdlJNTGlMZEV3dlg0K2RjTEVrY1dpNU5TbzB6NFBnMzU5Q2J6dzV5S25GR2pERVpKY2taYndOMG1ZRFcxUUZqQmFrUEREKzdCaFltZz09IiwibWFjIjoiMmJhYjBmNmMzMWQwNTIwMTFjMGFiZGJkY2MyNWRiYzAzMzU3OGU4MDliZGFlZWQzNzQ3MTNmOTcyYzY0NjMyNiJ9; expires=Sat, 09-May-2015 17:09:02 GMT; Max-Age=7200; path=/; httponly
Benchmark TEST
I tried to load one of my file css/bootstrap.min.css
ab -c 20 -n 1000 http://45.33.69.160/css/bootstrap.min.css
Result:
This is ApacheBench, Version 2.3 <$Revision: 1638069 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 45.33.69.160 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software: nginx/1.6.3
Server Hostname: 45.33.69.160
Server Port: 80
Document Path: /css/bootstrap.min.css
Document Length: 78358 bytes
Concurrency Level: 20
Time taken for tests: 0.239 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 78593000 bytes
HTML transferred: 78358000 bytes
Requests per second: 4184.42 [#/sec] (mean)
Time per request: 4.780 [ms] (mean)
Time per request: 0.239 [ms] (mean, across all concurrent requests)
Transfer rate: 321157.98 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 2 0.7 2 3
Processing: 1 3 0.8 3 5
Waiting: 0 1 0.8 1 4
Total: 3 5 0.7 5 7
Percentage of the requests served within a certain time (ms)
50% 5
66% 5
75% 5
80% 5
90% 6
95% 6
98% 6
99% 7
100% 7 (longest request)
Note: Time taken for tests: 0.239 seconds
The loading time of the content seem to be the same with/without my static content configuration. Isn't it suppose to be faster ?
For some reason, the configuration doesn't seem to be set at all.
Can someone please point out what I did wrong here ?
Suggestions or Feedbacks will be much appreciated !
Related
My frontend is trying to fetch() an image from my API, which usually handles my CORS. Because it directly requests the image, circumventing the API, I receive this issue: (which makes complete sense why)
Access to XMLHttpRequest at 'https://my-api.test/storage/media/1/15/poster.jpg' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I need to adjust my Nginx settings so that any file requested living under the /storage/ path will have this header added.
I must be misunderstanding parts of how I should configure Nginx in this case, because I was believing that this location-block should have worked:
location /storage/ {
add_header 'Access-Control-Allow-Origin' '*';
}
Oddly, it returns a "404 status":
curl -I https://my-api.test/storage/test-media/butterfly.jpg
HTTP/2 404
server: nginx/1.19.4
date: Tue, 17 Nov 2020 06:57:23 GMT
content-type: image/jpeg
content-length: 116501
etag: "5f6045f4-1c715"
On the other hand, if I disable the location-block condition:
#location /storage/ {
add_header 'Access-Control-Allow-Origin' '*';
#}
The header then is properly added:
curl -I https://my-api.test/storage/test-media/butterfly.jpg
HTTP/2 200
server: nginx/1.19.4
date: Tue, 17 Nov 2020 07:00:53 GMT
content-type: image/jpeg
content-length: 116501
last-modified: Tue, 15 Sep 2020 04:41:24 GMT
etag: "5f6045f4-1c715"
access-control-allow-origin: *
accept-ranges: bytes
Any help would be truly appreciated!
edit 1
I am not sure whether it makes a difference, but the "storage" folder is a symlink.
edit 2
The nginx error:
2020/11/17 12:45:50 [error] 64990#0: *1 open() "/storage/media/1/1/turtle.jpg" failed (2: No such file or directory), client: 127.0.0.1, server: , request: "HEAD /storage/media/1/1/turtle.jpg HTTP/1.1", host: "my-api.test"
When I try to visit the URL in the browser my-api.test/storage/media/1/1/turtle.jpg it returns the image and not a 404.
The solution to this problem was rather simple:
location /storage/ {
root '/Users/chrisbreuer/code/my-api/public';
add_header 'Access-Control-Allow-Origin' "*" always;
}
As you can see, the location-block is now adding a root which is where the "storage" folder lives in.
In my case, I was using Laravel Valet and when you have a secure site, you would need to adjust the nginx config file which is located in this path: ~/.config/valet/Nginx/meema-api.test.
Now, simply run valet restart and all files requested from within the storage-folder will have the header included.
In /etc/php/7.1/fpm/pool.d/www.conf I have set pm.status_path = /status. And in Nginx I have the following site configuration /etc/nginx/sites-enabled/datadog
server {
listen 80;
server_name localhost;
location ~ ^/(status|ping)$ {
access_log off;
allow 127.0.0.1;
deny all;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
}
}
I have tested that the status page is correctly setup using
SCRIPT_NAME=/status \
SCRIPT_FILENAME=/status \
REQUEST_METHOD=GET \
cgi-fcgi -bind -connect /run/php/php7.1-fpm.sock
Which returns
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Cache-Control: no-cache, no-store, must-revalidate, max-age=0
Content-type: text/plain;charset=UTF-8
pool: www
process manager: dynamic
start time: 13/Nov/2017:22:05:44 +0000
start since: 5030
accepted conn: 1789
listen queue: 0
max listen queue: 0
listen queue len: 0
idle processes: 2
active processes: 1
total processes: 3
max active processes: 4
max children reached: 0
slow requests: 0
However, if I try to access the Nginx site using curl http://localhost/status I get the following error.
2017/11/13 23:32:57 [error] 885#885: *35 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /status HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.1-fpm.sock:", host: "localhost"
You are referencing $document_root but there is no root directive in your server block
Hi Guys I want my all NON-WWW url request to move permanently and also rewrite to WWW and I have tried to follow existing solutions at these as well
Nginx no-www to www and www to no-www
but still it did not work for me.
e.g. I want example.com or example.com/* to rewrite to www.example.com or www.example.com/*
I am running PHP-FPM with nginx and memcache
below is my config
server {
listen 80;
server_name abc.com;
return 301 http://www.example.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
root /srv/www/abc;
index index.php index.html index.htm;
.......
}
Below is my curl response
neel:~ nilesh$ curl -I http://example.com
HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Fri, 21 Aug 2015 19:00:54 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/5.5.9-1ubuntu4.11
X-Drupal-Cache: HIT
Etag: "1440178291-0"
Content-Language: en
X-Generator: Drupal 7 (http://drupal.org)
Link: <http://example.com/>; rel="canonical",<http://example.com/>; rel="shortlink"
Cache-Control: public, max-age=1800
Last-Modified: Fri, 21 Aug 2015 17:31:31 +0000
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Vary: Cookie
Vary: Accept-Encoding
I have finally SOLVED my issue. I checked my nginx.conf and it was pointing to /etc/nginx/sites-enabled and /etc/nginx/conf.d
my nginx.conf->
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
I finally copied my file from sites-available to folder sites-enabled. infact I kept it synced.
below is the code i used under my server tag
server {
listen 80;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
#listen [::]:80 default_server ipv6only=on;
root /srv/www/example;
#rest config goes below
.......
Now all my non-www traffic is 301 permanently moved and rewrite to www with the above code.
I made a curl call to non-www and I got the following correct response.
neel:~ nilesh$ curl -I http://example.com
HTTP/1.1 301 Moved Permanently
Server: nginx/1.4.6 (Ubuntu)
Date: Thu, 27 Aug 2015 08:39:38 GMT
Content-Type: text/html
Content-Length: 193
Connection: keep-alive
Location: http://www.example.com/
I host some websites on my VPS, some "static" and some dynamic (WordPress). The static websites (static PHP pages) "respect" the headers I set in nginx conf, http section. Example:
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";
add_header X-Cache $upstream_cache_status;
Header:
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 16 Sep 2014 17:09:04 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Cache: HIT
Strict-Transport-Security: max-age=31536000; includeSubdomains;
WordPress websites instead don't have these headers I set:
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 16 Sep 2014 17:08:25 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
X-Pingback: http://website.com/xmlrpc.php
Link: <http://wp.me/P4zIfv-2>; rel=shortlink
X-UA-Compatible: IE=Edge,chrome=1
The two websites have the same vhost config! Of course liste, server_name, index ecc.. and then the locations:
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_cache website.com;
fastcgi_cache_valid 200 20m;
fastcgi_cache_bypass $no_cache;
fastcgi_no_cache $no_cache;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
Why does this happen with WP?
See: nginx add_header not working
"Second issue was that the location / {} block I had in place was actually sending nginx to the other location ~* (.php)$ block (because it would repath all requests through index.php, and that actually makes nginx process this php block). So, my add_header directives inside the first location directive were useless, and it started working after I put all the directives I needed inside the php location directive."
See also: https://gist.github.com/adityamenon/6753574
So put them INSIDE your location block
i.e to replace Apache with a PHP application that sent back html files when http requests for .php files are sent?
How practical is this?
It's already been done but if you want to know how practical it is, then i suggest you install and test with Apache bench to see the results:
http://nanoweb.si.kz/
Edit, A benchmark from the site:
Server Software: aEGiS_nanoweb/2.0.1-dev
Server Hostname: si.kz
Server Port: 80
Document Path: /six.gif
Document Length: 28352 bytes
Concurrency Level: 20
Time taken for tests: 3.123 seconds
Complete requests: 500
Failed requests: 0
Broken pipe errors: 0
Keep-Alive requests: 497
Total transferred: 14496686 bytes
HTML transferred: 14337322 bytes
Requests per second: 160.10 [#/sec] (mean)
Time per request: 124.92 [ms] (mean)
Time per request: 6.25 [ms] (mean, across all concurrent requests)
Transfer rate: 4641.91 [Kbytes/sec] received
Connnection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 1.9 0 13
Processing: 18 100 276.4 40 2739
Waiting: 1 97 276.9 39 2739
Total: 18 100 277.8 40 2750
Percentage of the requests served within a certain time (ms)
50% 40
66% 49
75% 59
80% 69
90% 146
95% 245
98% 449
99% 1915
100% 2750 (last request)
Apart from Nanoweb, there is also a standard PEAR component to build standalone applications with a built-in webserver:
http://pear.php.net/package/HTTP_Server
Likewise the upcoming PHP 5.4 release is likely to include an internal mini webserver which facilitates simple file serving. https://wiki.php.net/rfc/builtinwebserver
php -S localhost:8000
Why reinvent the wheel? Apache or any other web server has had a lot of work put into it by a lot of skilled people to be stable and to do everything you wanted it to do.
Just FYI, PHP 5.4 just released with in-built webserver. Now you can run a local server with very simple commands like -
$ cd ~/public_html
$ php -S localhost:8000
And you'll see the requests and responses like this -
PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011
Listening on localhost:8000
Document root is /home/me/public_html
Press Ctrl-C to quit.
[Thu Jul 21 10:48:48 2011] ::1:39144 GET /favicon.ico - Request read
[Thu Jul 21 10:48:50 2011] ::1:39146 GET / - Request read
[Thu Jul 21 10:48:50 2011] ::1:39147 GET /favicon.ico - Request read
[Thu Jul 21 10:48:52 2011] ::1:39148 GET /myscript.html - Request read
[Thu Jul 21 10:48:52 2011] ::1:39149 GET /favicon.ico - Request read