User Tools

Site Tools


nginx

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

nginx [2017/01/05 06:07] (current)
felixonmars created
Line 1: Line 1:
 +==== Installation on Ubuntu Server ====
 +<​code>​
 +apt install -y software-properties-common
 +add-apt-repository -y ppa:​nginx/​stable
 +apt update && apt install nginx -y
 +</​code>​
  
 +==== Installation on Debian Server ====
 +<​code>​
 +wget http://​www.dotdeb.org/​dotdeb.gpg -O - | apt-key add -
 +
 +tee /​etc/​apt/​sources.list.d/​dotdeb.list << EOF
 +deb http://​mirror.us.leaseweb.net/​dotdeb/​ wheezy all
 +deb-src http://​mirror.us.leaseweb.net/​dotdeb/​ wheezy all
 +EOF
 +
 +apt update && apt install nginx -y
 +</​code>​
 +
 +==== Proxy ====
 +<​code>​
 +resolver 8.8.4.4;
 +server {
 +    listen 3128;
 +    location / {
 +        proxy_pass http://​$http_host$request_uri;​
 +    }
 +}
 +</​code>​
 +
 +==== Reverse Proxy ====
 +<​code>​
 +server {
 +    listen 80;
 +    server_name new-site.com;​
 +
 +    location / {
 +        proxy_pass http://​origin-site.com/;​
 +        proxy_redirect default;
 +        proxy_set_header Host $host;
 +        proxy_set_header X-Real-IP $remote_addr;​
 +        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;​
 +        port_in_redirect ​   on;
 +        server_name_in_redirect off;
 +        proxy_connect_timeout 300;
 +    }
 +}
 +</​code>​
 +
 +==== Cached Reverse Proxy ====
 +Make directories for cache:
 +<​code>​
 +mkdir -p /​var/​cache/​nginx/​cache
 +mkdir -p /​var/​cache/​nginx/​temp
 +</​code>​
 +
 +Paste below `log_format` directive in `nginx.conf`:​
 +<​code>​
 +client_body_buffer_size ​ 512k;
 +proxy_connect_timeout ​   5;
 +proxy_read_timeout ​      60;
 +proxy_send_timeout ​      5;
 +proxy_buffer_size ​       16k;
 +proxy_buffers ​           4 64k;
 +proxy_busy_buffers_size 128k;
 +proxy_temp_file_write_size 128k;
 +proxy_temp_path ​  /​var/​cache/​nginx/​temp;​
 +proxy_cache_path ​ /​var/​cache/​nginx/​cache levels=1:2 keys_zone=cache_one:​500m inactive=7d max_size=30g;​
 +</​code>​
 +
 +Add to vhost configuration:​
 +<​code>​
 +proxy_cache cache_one;
 +proxy_cache_valid ​ 200 304 3d;
 +proxy_cache_key $host$uri$is_args$args;​
 +expires 10d;
 +</​code>​
 +
 +==== Set Expires Headers For Static Content ====
 +<​code>​
 +location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
 +    expires 1y;
 +    log_not_found off;
 +}
 +</​code>​
 +
 +==== Increase HTTP Post Size Limit ====
 +<​code>​
 +http {
 +    #...
 +    client_max_body_size 100m;
 +    #...
 +}
 +</​code>​
 +
 +==== Set Correct Files/​Folders Permissions ====
 +<​code>​
 +chown -R www-data:​www-data .
 +find . -type f -exec chmod 644 {} \;
 +find . -type d -exec chmod 755 {} \;
 +</​code>​
 +
 +References:
 +
 +  * http://​wiki.ptsang.net/​Debian%E6%9C%8D%E5%8A%A1%E9%85%8D%E7%BD%AE
 +  * http://​nicholaskuechler.com/​2011/​04/​24/​nginx-how-to-set-expires-headers-for-images/​
 +  * http://​www.vpsmm.com/​blog-2522.html
 +  * http://​rtcamp.com/​wordpress-nginx/​tutorials/​php/​increase-file-upload-size-limit/​
nginx.txt ยท Last modified: 2017/01/05 06:07 by felixonmars