Created
February 18, 2015 12:24
-
-
Save meglio/4870ce6bc93f3a33528f to your computer and use it in GitHub Desktop.
Nginx website virtual host example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# WWW HTTP(S) -> non-WWW | |
server { | |
listen 80; | |
#listen 443 ssl; | |
server_name www.EXAMPLE.COM; | |
return 301 $scheme://EXAMPLE.COM$request_uri; | |
#ssl_certificate /home/USER/.ssl/ssl-unified.crt; | |
#ssl_certificate_key /home/USER/.ssl/ssl.key; | |
} | |
# HTTP -> HTTPS | |
#server { | |
# listen 80; | |
# server_name .EXAMPLE.COM; | |
# return 301 https://$host$request_uri; | |
#} | |
# HTTPS / HTTP | |
server { | |
listen 80; | |
#listen 443 ssl; | |
server_name EXAMPLE.COM cookie-free.EXAMPLE.COM; | |
set $host_homedir /home/USER/web; | |
server_name_in_redirect off; | |
client_max_body_size 1m; | |
#ssl_certificate /home/USER/.ssl/ssl-unified.crt; | |
#ssl_certificate_key /home/USER/.ssl/ssl.key; | |
set $host_without_www $host; | |
if ($host ~* ^www\.(.*)){ | |
set $host_without_www $1; | |
} | |
if ($host_without_www = ''){ | |
set $host_without_www $host; | |
} | |
# Cookies free | |
if ($host ~* ^cookie-free\.(.*)){ | |
set $host_without_www $1; | |
} | |
# Restricted folders | |
location ~* /(\_|\.)(.*){ | |
return 404; | |
} | |
# Redirect abc.123.js to abc.js | |
rewrite (?i)^(.*)\.\d+\.(jpg|gif|jpeg|png|css|js|doc|docs|pdf|txt|csv) $1.$2; | |
# Cache static files | |
location ~* \.(jpg|jpeg|gif|png|css|ico|exe|zip|rar|pdf|htc|css|js|jsp|tiff|gzip|tar|svg|swf|flv|fla)$ { | |
root $host_homedir/; | |
expires modified +30d; | |
} | |
# Route request to PHP | |
location / { | |
root $host_homedir/; | |
if (!-e $request_filename) { | |
rewrite ^(.*) /index.php?_REQUEST_URI=$1 last; | |
} | |
index index.php index.html index.htm; | |
} | |
# FASTCGI params | |
fastcgi_param GATEWAY_INTERFACE CGI/1.1; | |
fastcgi_param SERVER_SOFTWARE nginx; | |
fastcgi_param QUERY_STRING $query_string; | |
fastcgi_param REQUEST_METHOD $request_method; | |
fastcgi_param CONTENT_TYPE $content_type; | |
fastcgi_param CONTENT_LENGTH $content_length; | |
fastcgi_param SCRIPT_FILENAME $host_homedir$fastcgi_script_name; | |
fastcgi_param SCRIPT_NAME $fastcgi_script_name; | |
fastcgi_param REQUEST_URI $request_uri; | |
fastcgi_param DOCUMENT_URI $document_uri; | |
fastcgi_param DOCUMENT_ROOT $document_root; | |
fastcgi_param SERVER_PROTOCOL $server_protocol; | |
fastcgi_param REMOTE_ADDR $remote_addr; | |
fastcgi_param REMOTE_PORT $remote_port; | |
fastcgi_param SERVER_ADDR $server_addr; | |
fastcgi_param SERVER_PORT $server_port; | |
fastcgi_param SERVER_NAME $server_name; | |
fastcgi_param REDIRECT_STATUS 200; | |
#fastcgi_param HTTPS on; | |
# Fast CGI PHP | |
location ~ \.php$ { | |
root $host_homedir/; | |
if (!-e $request_filename) { | |
return 404; | |
} | |
# With php5-fpm: | |
fastcgi_pass unix:/var/run/php5-fpm.sock; | |
fastcgi_index index.php; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment