Nginx

From KevinWiki

(Difference between revisions)
Jump to: navigation, search
Line 19: Line 19:
   more_set_headers 'Server: server';
   more_set_headers 'Server: server';
 +
=== Virtual Host ===
 +
==== Default ====
 +
<pre>
 +
server {
 +
  listen      80;
 +
  listen      127.0.0.1:80;
 +
  server_name localhost;
 +
  root        /var/www;
 +
  index      index.html index.htm;
 +
 +
  location / {
 +
    index    index.html index.htm;
 +
  }
 +
}
 +
</pre>
 +
 +
* Restart Nginx
 +
$ sudo /etc/init.d/nginx restart
== PHP ==
== PHP ==
  $ sudo apt-get install php5-fpm
  $ sudo apt-get install php5-fpm
 +
 +
<pre>
 +
server {
 +
  listen      80;
 +
  listen      127.0.0.1:80;
 +
  server_name localhost;
 +
  root        /var/www;
 +
  index      index.php index.html index.htm;
 +
 +
  location / {
 +
    index    index.php index.html index.htm;
 +
  }
 +
 +
  location ~ \.php$ {
 +
    try_files $uri =404;
 +
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
 +
    fastcgi_pass 127.0.0.1:9000;
 +
    fastcgi_index index.php;
 +
    include fastcgi_params;
 +
  }
 +
}
 +
</pre>
 +
 +
* Restart php5-fpm
 +
$ sudo /etc/init.d/php5-fpm
 +
 +
 +
== Use with Tomcat ==
 +
 +
<pre>
 +
server {
 +
  listen      80;
 +
  listen      127.0.0.1:80;
 +
  server_name localhost;
 +
  root        /var/www;
 +
 +
  location /manager {
 +
    proxy_pass              http://localhost:8080;
 +
    proxy_set_header        X-Real-IP $remote_addr;
 +
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
 +
    proxy_set_header        Host $http_host;
 +
  }
 +
}
 +
</pre>
 +
 +
* Add a new file to <code>/etc/nginx/sites-available/</code>
 +
<pre>
 +
server {
 +
  listen          80;
 +
  listen          127.0.0.1:80;
 +
  server_name    demo.localhost;
 +
  root            /path/to/tomcat/webapps;
 +
 
 +
  location / {
 +
    proxy_pass              http://localhost:8080;
 +
    proxy_set_header        X-Real-IP $remote_addr;
 +
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
 +
    proxy_set_header        Host $http_host;
 +
  }
 +
 +
  location ^/$ {
 +
    proxy_pass              http://localhost:8080/default-app;
 +
    proxy_set_header        X-Real-IP $remote_addr;
 +
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
 +
    proxy_set_header        Host $http_host;
 +
  }
 +
}
 +
</pre>
 +
 +
 +
 +
== Example ==
 +
<pre>
 +
server {
 +
  listen      80;
 +
  listen      127.0.0.1:80;
 +
  server_name localhost;
 +
  root        /var/www;
 +
  index      index.php index.html index.htm;
 +
 +
  location / {
 +
    index    index.php index.html index.htm;
 +
  }
 +
 +
  location /manager {
 +
    proxy_pass              http://localhost:8080;
 +
    proxy_set_header        X-Real-IP $remote_addr;
 +
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
 +
    proxy_set_header        Host $http_host;
 +
  }
 +
 +
  location ~ \.php$ {
 +
    try_files $uri =404;
 +
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
 +
    fastcgi_pass 127.0.0.1:9000;
 +
    fastcgi_index index.php;
 +
    include fastcgi_params;
 +
  }
 +
}
 +
</pre>

Revision as of 05:59, 31 December 2012

Contents

Nginx

NginX is a lightweight open source Web server .

Installation

$ sudo apt-get install nginx

Configuration

Remove Server Info

Version

Modify /etc/nginx/nginx.conf

  server_tokens off;

Server Info in Response Header

  • Remove server info in the response header

Install extra modules for changing header

$ sudo apt-get install nginx-extras 
  • Add the following line to the http block in the /etc/nginx/nginx.conf.
  more_set_headers 'Server: server';

Virtual Host

Default

server {
  listen      80;
  listen      127.0.0.1:80;
  server_name localhost;
  root        /var/www;
  index       index.html index.htm;

  location / {
    index     index.html index.htm;
  }
}
  • Restart Nginx
$ sudo /etc/init.d/nginx restart 

PHP

$ sudo apt-get install php5-fpm
server {
  listen      80;
  listen      127.0.0.1:80;
  server_name localhost;
  root        /var/www;
  index       index.php index.html index.htm;

  location / {
    index     index.php index.html index.htm;
  }

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
  }
}
  • Restart php5-fpm
$ sudo /etc/init.d/php5-fpm 


Use with Tomcat

server {
  listen      80;
  listen      127.0.0.1:80;
  server_name localhost;
  root        /var/www;

  location /manager {
    proxy_pass              http://localhost:8080;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        Host $http_host;
  }
}
  • Add a new file to /etc/nginx/sites-available/
server {
  listen          80;
  listen          127.0.0.1:80;
  server_name     demo.localhost;
  root            /path/to/tomcat/webapps;
  
  location / {
    proxy_pass              http://localhost:8080;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        Host $http_host;
  }

  location ^/$ {
    proxy_pass              http://localhost:8080/default-app;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        Host $http_host;
  }
}


Example

server {
  listen      80;
  listen      127.0.0.1:80;
  server_name localhost;
  root        /var/www;
  index       index.php index.html index.htm;

  location / {
    index     index.php index.html index.htm;
  }

  location /manager {
    proxy_pass              http://localhost:8080;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        Host $http_host;
  }

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
  }
}
Personal tools