\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{dm0610} \pdfinfo{ /Title (nginx-basics.pdf) /Creator (Cheatography) /Author (dm0610) /Subject (Nginx basics Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{5F7EA3} \definecolor{LightBackground}{HTML}{F5F6F9} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Nginx basics Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{dm0610} via \textcolor{DarkBackground}{\uline{cheatography.com/77761/cs/19035/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}dm0610 \\ \uline{cheatography.com/dm0610} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 13th March, 2019.\\ Updated 13th March, 2019.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Install with apt, yum}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#for ubuntu \newline sudo apt-get update -y \& sudo apt-get install -y nginx \& sudo systemctl enable nginx \& sudo systemctl start nginx \newline \#for Centos \newline sudo yum update -y \& sudo yum install -y epel-release \& sudo yum install -y nginx \& sudo systemctl enable nginx \& sudo systemctl start nginx} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Creating a Virtual Hosts}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# make copy of /etc/nginx/nginx.conf and begin edit current conf file \newline server\{ \newline listen 80; \newline server\_name 192.168.1.1 \newline root /sites/demo \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{file types/ mime.types}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#in http section \newline include mime.types; \newline types\{ \newline text/css css; \newline text/http http; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Locations}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#Preferential prefix match \newline location \textasciicircum{}\textasciitilde{} /hello \{ \newline return 200 'Hello'; \newline \} \newline \#exact match \newline location = /hello \{ \newline return 200 'Hello'; \newline \} \newline \#regex match - case sensitive \newline location \textasciitilde{} /hello{[}1-9{]} \{ \newline return 200 'Hello'; \newline \} \newline \#regex match - case insensitive \newline location \textasciitilde{}* /hello{[}1-9{]} \{ \newline return 200 'Hello'; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{buffers and Timeouts}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#Buffer size for POST submissions \newline client\_body\_buffer\_size 10K; \newline client\_max\_body\_size 8m; \newline \#Buffer size for HEADERS \newline client\_header\_buffer\_size 1K; \newline \#MAX time to receive client headers/body \newline client\_body\_timeout 12; \newline client\_header\_timeout 12; \newline \#MAX time to keep a connection open for \newline keepalive\_timeout 15; \newline \#MAX time for client accept/receive a response \newline send\_timeout 10; \newline \#skip buffering for static files \newline sendfile 10; \newline \#optimize sendfile packets \newline tcp\_nopush 10;} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Source code build}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{sudo apt-get update -y \newline wget \seqsplit{http://nginx.org/download/nginx\_cur\_ver.tar.gz} \newline tar -zxvf nginx\_cur\_ver.tar.gz \newline \#for ubuntu\# \newline sudo apt-get install -y build-essential \#install builder \newline sudo apt-get install -y libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev \newline \#for centos\# \newline sudo yum install -y groupinstall "Development Tools" \newline sudo yum install -y pcre pcre-devel zlib zlib-devel openssl openssl-devel \newline \# in unpacked location \newline ./configure -{}-help \newline sudo ./configure -{}-sbin-path=/usr/bin/nginx -{}-conf-path=/etc/nginx/nginx.conf -{}-error-log-path=/var/log/nginx/error.log -{}-http-log-path=/var/log/nginx/access.log -{}-with-pcre -{}-pid-path=/var/run/nginx.pid -{}-with-http\_ssl\_module \newline sudo make \& sudo make install \& nginx -V} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Variables}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{set}} \$weekend 'No'; \newline if (\$date\_local \textasciitilde{} 'Saturday|Sunday')\{ \newline {\bf{set}} \$weekend 'Yes';\} \newline \$host, \$http\_host, \$https, \$request\_method, \$request\_uri, \$scheme, \$server\_name, \$server\_port, \$args, \$arg\_name, \$uri} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Rewrites \& Redirects}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# Simple redirect \newline location /logo\{ \newline return 307 /logo.png; \newline \} \newline \#simple rewrite \newline rewrite \textasciicircum{}/user/(\textbackslash{}w+)/(something) /greet/\$1 \$2; \newline rewrite \textasciicircum{}/rewriteme/(.*)\$ /\$1 last;} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{try\_files}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{root /var/www/main; \newline try\_files \$uri \$uri.html \$uri/ /fallback/index.html;} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{dynamic modules}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{load\_module \seqsplit{modules/nginx\_http\_image\_filter\_module}.so; \newline http\{server\{location=logo.png\{image\_filter rotate 180;\}\}\}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{headers\&expires}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{location \textasciitilde{}* \textbackslash{}.(jpg|png|js) \{ \newline access\_log off; \newline add\_header Cache-Control public; \newline add\_header Pragma public; \newline add\_header Vary Accept-Encoding; \newline expires 1M;\}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Adding nginx service if source-code}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Create file \seqsplit{/lib/systemd/system/nginx}.service \newline add and edit for current passes: \newline {[}Unit{]} \newline Description=The NGINX HTTP and reverse proxy server \newline After=syslog.target network.target remote-fs.target nss-lookup.target \newline {[}Service{]} \newline Type=forking \newline PIDFile=/run/nginx.pid \newline ExecStartPre=/usr/sbin/nginx -t \newline ExecStart=/usr/sbin/nginx \newline ExecReload=/usr/sbin/nginx -s reload \newline ExecStop=/bin/kill -s QUIT \$MAINPID \newline PrivateTmp=true \newline {[}Install{]} \newline WantedBy=multi-user.target} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\seqsplit{https://www.nginx.com/resources/wiki/start/topics/examples/systemd/}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{PHP Processing}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{sudo apt-get update -y \& sudo apt-get -y install php-fpm \& sudo enable php7.2-fpm \newline user www-data \newline server\{ \newline index index.php index.html; \newline location / \{ \newline try\_files \$uri \$uri/ =404; \newline \} \newline location \textasciitilde{}\textbackslash{}.php\$\{ \newline \#pass php request to the php-fpm service(fastcgi) \newline include fastcgi.conf \newline fastcgi\_pass \seqsplit{unix:/run/php/php7.2-fpm.sock} \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{worker\_processes}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{worker\_processes auto; \newline events \{ \newline worker\_connections 1024;\} \newline \# yuo can test connection\_per\_core limit by 'ulimit -n' command \newline pid /var/run/new\_nginx.pid;} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Compressed Responses with gzip}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{http\{ \newline gzip on; \newline gzip\_comp\_level 4; \newline gzip\_types /text/css /text/javascript;\}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{FASTCGI Cache}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#configure microcache (fastcgi) \newline fastcgi\_cache\_path /tmp/nginx/cache levels=1:2 keys\_zone=ZONE\_1:100m inactive=60m; \newline fastcgi\_cache\_key \seqsplit{"\$scheme\$request\_method\$host\$request\_uri";} \newline add\_header X-Cache \seqsplit{\$upstream\_cache\_status;} \newline location \textasciitilde{}\textbackslash{}.php\$ \{ \newline include fastcgi.conf; \newline fastcgi\_pass \seqsplit{unix:/run/php/php7.2-fpm.sock;} \newline \#enabling cahe \newline fastcgi\_cahe ZONE\_1; \newline fastcgi\_cahe\_valid 200 60m; \newline fastcgi\_cahe\_valid 404 10m; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{LoadBallancing}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{upstream balanser \{ \newline server 192.168.1.1:80; \newline server 192.168.1.2:80;\} \newline server \{ \newline listen 192.168.1.3:80; \newline server\_name your\_site.com; \newline error\_log \seqsplit{/var/log/nginx/your\_site}.com\_error.log; \newline location / \{ \newline proxy\_pass http://balancer; \newline \} \newline \} \newline ip\_hash; \# freeze connection from one host to one server \newline least\_conn; \# connection to server with less-loading} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}