Nginx server Configure & Compile
We can use ppa:nginx that is even mentioned in official install documentation, and then compile our extra modules in it as in procedure explained here.
I prefer building it from source, so we will do it in that way.
Also, make sure we have all the packages needed for compilation:
# needed packages
sudo apt install -y build-essential zlib1g-dev libpcre3 libpcre3-dev unzip uuid-dev git
# also required
sudo apt install -y libxslt-dev libgd-dev libgeoip-dev checkinstall
# needed for Google BoringSSL (HTTP/3 patch)
sudo apt install -y curl git cmake ninja-build golang libpcre3-dev zlib1g-dev
# Also need latest Rust (and Cargo), not the ones from Ubuntu repos
curl https://sh.rustup.rs -sSf | sh
Download nginx modules
I usually update everything before continuing:
apt update; apt -y upgrade; apt dist-upgrade; apt -y autoremove; apt clean
Copy-pasting all this will probably work:
|
|
Done. The new package has been installed and saved to /usr/local/src/nginx-1.11.5/nginx-1.11.5-from-source_1.11.5-1_amd64.deb You can remove it from your system anytime using: nginx-1.11.5-from-source
If you didn’t previously have a version of nginx installed from source, maybe you’ll need to set up init scripts. Or not?.
Enable dynamic modules
echo ’load_module modules/ngx_pagespeed.so;’ > /usr/share/nginx/modules-available/mod-pagespeed.conf echo ’load_module modules/ngx_http_headers_more_filter_module.so;’ > /usr/share/nginx/modules-available/mod-http-headers-more-filter.conf sudo ln -s /usr/share/nginx/modules-available/mod-pagespeed.conf /etc/nginx/modules-enabled/ sudo ln -s /usr/share/nginx/modules-available/mod-http-headers-more-filter.conf /etc/nginx/modules-enabled/
A MUST! h5bp/server-configs-nginx
Nginx settings.
FuelPHP-Nginx/nginx at master · rajibmp/FuelPHP-Nginx
server { listen 80 default_server; listen 443 default_server ssl;
client_max_body_size 10M;
root /usr/share/nginx/html;
index index.php;
# Handle images directly
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf)$ {
root /usr/share/nginx/html;
}
location /
index index.html index.php;
try_files $uri $uri/ @handler;
expires 30d;
}
location /. {
return 404;
}
location @handler {
rewrite / /index.php;
}
location ~ \.php/ {
rewrite ^(.*\.php)/ $1 last;
}
# All Requests
location ~ \.php$ {
## Catch 404s that try_files miss
if (!-e $request_filename) {
rewrite / /index.php last;
}
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass <strong>php:9000</strong>;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_buffer_size 1024k;
fastcgi_buffers 500 512k;
fastcgi_connect_timeout 1200;
fastcgi_send_timeout 1200;
fastcgi_read_timeout 1200;
}
gzip on;
gzip_min_length 1000;
gzip_proxied any;
}
Configuring Fast CGI Params for Multi-Site
Default fastcgi_params file that is installed on a fresh nginx server has this line:
fastcgi_param SERVER_NAME $server_name;
Update the fastcgi_param
to:
fastcgi_param SERVER_NAME $host;
Nginx uses $_SERVER['server_name']
as the first server listed in the
virtual server config. Way to get around this is to setup the FastCGI
params to use host instead of server_name.
Why don’t we use $http_host
variable?
Let’s explain these nginx variables a little:
-
$host
equals$http_host
, lowercase and without the port number, and when there is noHTTP_HOST
header then it equals to$server_name
. -
$http_host
equals always theHTTP_HOST
request header. -
$server_name
is the first server_name that is defined in the current server block. If you have multiple server_names, only the first one will appear. If you happen to have a regex in the first one, that becomes the$server_name
and all sorts of ugly stuff can happen.
Sources & Articles
Change SERVER_NAME
from $server_name
to $host
in global fastcgi_param
sed -i -r 's/^(.*SERVER_NAME\s+).*$/\n# Multi-site fix\n\1$host;/gi' /etc/nginx/fastcgi_params
502 Bad Gateway: Upstream sent big headers
Started noticing when you try to logout from system, nginx spits out the dreaded 502 Bad Gateway message.
I have discovered a problem in error.log
, and it is about
communication between nginx and PHP.
upstream sent too big header while reading response header from upstream
Bug happened on all the servers (thetube, hotmess & ganzohr), except Plesk where they already have a little increased buffer values.
Solution?
How to determine optimal values?
Everything explained:
- Tweaking fastcgi-buffers - EasyEngine
- nginx: fix 502 gateway error with fastcgi_buffers - Marcel Zurreck
- php - Nginx 502 Bad Gateway. Solved by increasing buffer. Why? - Stack Overflow
Read and you will be enlightened:
The maximum response size:
awk '($9 ~ /200/)' access.log | awk '{print $10}' | sort -nr | head -n 1
Average response size:
echo $(( `awk '($9 ~ /200/)' access.log | awk '{print $10}' | awk '{s+=$1} END {print s}'` / `awk '($9 ~ /200/)' access.log | wc -l` ))
I’ve got on save-up.ch: max: 396k, avg: 46k
Put inside http { }
of nginx.conf
section as we want a global
solution.
|
|
Woow: what a resource: https://www.scalescale.com/tips/nginx/
|
|
wget https://raw.githubusercontent.com/h5bp/server-configs-nginx/master/h5bp/web_performance/cache_expiration.conf -O /etc/nginx/conf.d/cache_expiration.conf
Force www
if ($host !~* ^(www)) { return 301 $scheme://www.$host$request_uri; }
Force non-www
if ($host ~* ^www.(.*)$) { return 301 $scheme://$1$request_uri; }
Force SSL
if ($scheme = http) { return 301 https://$host$request_uri$is_args$args; }
Force non-SSL
if ($scheme = https) { return 301 http://$host$request_uri$is_args$args; }
Understanting nginx
Understanding Nginx Server and Location Block Selection Algorithms | DigitalOcean How it works: nginx and error pages
Plesk nginx configure
Mine configuration:
First part are original compile options on Plesk, and after that - I added mine.
./configure \
--prefix=/usr/share \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--lock-path=/var/lock/nginx.lock \
--pid-path=/var/run/nginx.pid \
--http-client-body-temp-path=/var/lib/nginx/body \
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
--http-proxy-temp-path=/var/lib/nginx/proxy \
--http-scgi-temp-path=/var/lib/nginx/scgi \
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
--user=nginx \
--group=nginx \
--with-ipv6 \
--with-file-aio \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
\
--with-http_auth_request_module \
--with-http_addition_module \
--with-http_spdy_module \
--with-http_xslt_module \
--with-mail \
--with-mail_ssl_module \
--add-module=/usr/local/src/ngx_pagespeed-release-1.9.32.6-beta \
--add-module=/usr/local/src/ngx_cache_purge-2.3
# I removed the passenger module that was in Plesk original config
--add-module=/usr/share/passenger/ngx_http_passenger_module \
# Not using some of modules
--with-http_image_filter_module (requires GD library)
--with-http_geoip_module (requires GeoIP library)
Install Nginx, Passenger, PageSpeed with spdy on Ubuntu 14.04 « Jethro’s logs
How to server different PHP per folder
# Use HHVM
#
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
# For this location old PHP
location ^~ /hoertest-js/ {
try_files $uri $uri/ /hoertest-js/index.php$is_args$args;
location ~ \.php$ {
try_files /dummy_nonexistant_file @phpfpm;
}
}
location ~ \.php$ {
try_files /dummy_nonexistant_file @hhvm;
}
# HHVM with failover to php-fpm
#
location @hhvm {
fastcgi_pass ...
}
location @phpfpm {
fastcgi_pass ...
}
WordPress specific Nginx conf tweaks: pothi/WordPress-Nginx: WordPress specific Nginx conf tweaks - Please see the updated repo at Also here: nginx settings for WordPress.
Nginx Security
Top 20 Nginx WebServer Best Security Practices
Proverio sam i ovo:
–with-file-aio
Boosting NGINX Performance 9x with Thread Pools Boosting Nginx Performance with Thread Pools | Hacker News
Just add in the ‘http’, ‘server’, or ’location’ context
aio threads;
to nginx.conf or better yet, to /etc/nginx/conf.d/basics.conf
(server context)
–with-ipv6 Nginx: Configure and Install With IPv6 Networking Support
Optimized setup for Wordpress
WordOps/WordOps: Install and manage a high performance WordPress stack with a few keystrokes author and VirtuBox/ubuntu-nginx-web-server: EasyEngine/WordOps optimized configuration on Ubuntu 16/18.04 LTS Amazing!
Some interesting nginx configs: VirtuBox/nginx-config: Nginx optimized configurations with examples for some applications Contemporary: angristan/nginx-autoinstall: Compile NGINX from source with custom modules and patches on Debian and Ubuntu