Difference between revisions of "Nginx"

From vpsget wiki
Jump to: navigation, search
 
(14 intermediate revisions by 2 users not shown)
Line 18: Line 18:
 
  service httpd stop
 
  service httpd stop
 
  chkconfig httpd off
 
  chkconfig httpd off
 +
Main config file is ''/etc/nginx/nginx.conf''. Also you can add hosts and discribe them in additional conf files ''/etc/nginx/conf.d/*.conf''. You can base on example config file ''/etc/nginx/conf.d/default.conf''
 +
<h2>How to install nginx on Debian.</h2>
 +
First append nginx repo to /etc/apt/sources.list
 +
deb http://nginx.org/packages/debian/ squeeze nginx
 +
deb-src http://nginx.org/packages/debian/ squeeze nginx
 +
Now install nginx
 +
apt-get install nginx
 +
Make sure you haven't apache2 service running and listening the same port (80). You can display running services with the command:
 +
top
 +
If you want to turn off apache2 and disable it to start at boot, enter:
 +
service apache2 stop
 +
update-rc.d -f apache2 remove
 +
Service nginx is set to start at boot by default. To start it manually, enter:
 +
service nginx start
 +
Main config file is ''/etc/nginx/nginx.conf''. Also you can add hosts and discribe them in additional conf files ''/etc/nginx/conf.d/*.conf''.
 +
<h2>How to install nginx on Ubuntu.</h2>
 +
First append nginx repo to /etc/apt/sources.list
 +
deb http://nginx.org/packages/ubuntu/ lucid nginx
 +
deb-src http://nginx.org/packages/ubuntu/ lucid nginx
 +
Now install nginx
 +
apt-get install nginx
 +
Make sure you haven't apache2 service running and listening the same port (80). You can display running services with the command:
 +
top
 +
If you want to turn off apache2 and disable it to start at boot, enter:
 +
service apache2 stop
 +
update-rc.d -f apache2 remove
 +
Service nginx is set to start at boot by default. To start it manually, enter:
 +
service nginx start
 +
Main config file is ''/etc/nginx/nginx.conf''. Also you can add hosts and discribe them in additional conf files ''/etc/nginx/conf.d/*.conf''.
 +
<h2>How to configure nginx server blocks</h2>
 +
If you want to point several domains to one IP address you can use nginx server blocks.
 +
<h3>Two Server Blocks, Serving Static Files</h3>
 +
http {
 +
  index index.html;
 +
 +
  server {
 +
    server_name www.domain1.com;
 +
    access_log logs/domain1.access.log main;
 +
 +
    root /var/www/domain1.com/htdocs;
 +
  }
 +
 +
  server {
 +
    server_name www.domain2.com;
 +
    access_log  logs/domain2.access.log main;
 +
 +
    root /var/www/domain2.com/htdocs;
 +
  }
 +
}
 +
<h3>A Default "Catch All" Server Block</h3>
 +
http {
 +
  index index.html;
 +
 +
  server {
 +
    listen 80 default_server;
 +
    server_name _; # This is just an invalid value which will never trigger on a real hostname.
 +
    access_log logs/default.access.log main;
 +
 +
    server_name_in_redirect off;
 +
 +
    root  /var/www/default/htdocs;
 +
  }
 +
}
 +
<h3>Wildcard Subdomains in a Parent Folder</h3>
 +
  server {
 +
  # Replace this port with the right one for your requirements
 +
  listen 80 default_server;  #could also be 1.2.3.4:80
 +
 +
  # Multiple hostnames separated by spaces.  Replace these as well.
 +
  server_name star.yourdomain.com *.yourdomain.com; # Alternately: _
 +
 +
  root /PATH/TO/WEBROOT;
 +
 +
  error_page 404 errors/404.html;
 +
  access_log logs/star.yourdomain.com.access.log;
 +
 +
  index index.php index.html index.htm;
 +
 +
  # static file 404's aren't logged and expires header is set to maximum age
 +
  location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
 +
    access_log off;
 +
    expires max;
 +
  }
 +
 +
  location ~ \.php$ {
 +
    include fastcgi_params;
 +
    fastcgi_intercept_errors on;
 +
    # By all means use a different server for the fcgi processes if you need to
 +
    fastcgi_pass  127.0.0.1:YOURFCGIPORTHERE;
 +
  }
 +
 +
  location ~ /\.ht {
 +
    deny  all;
 +
  }
 +
}
 +
 +
 +
== Some troubleshooting ==
 +
 +
Problem: 403 Forbidden
 +
 +
Solution: For example if you have more than 1 domain at your nginx server and domain1 has links to domain2 which returns error 403 in browser, the problem could be in hotlinks.
 +
Open up your domain2 conf file and make sure it has domain1 in the list of valid referrers.
 +
server {
 +
            location ~* ^.+\.(gif|jpg|mpg|mp3|mpeg|avi)$ {
 +
                valid_referers none blocked www.domain1.com domain1.com;
 +
                if ($invalid_referer) {
 +
                    return  403;
 +
                }
 +
                root $root_path;
 +
            }
 +
        }
 +
 +
Source: [http://wiki.nginx.org/ServerBlockExample official nginx wiki]
 +
 +
<h2>Nginx reverse proxy example. </h2>
 +
Nginx reverse proxy: http://wiki.vpsget.com/index.php/Nginx_Reverse_Proxy
 +
 +
[[Category:Linux]]

Latest revision as of 19:53, 31 October 2016

How to install nginx on Centos.

Add nginx repo first. To add it, create the file /etc/yum.repos.d/nginx.repo with you favorite text editor, for example:

nano /etc/yum.repos.d/nginx.repo

Then insert the strings below and save the file:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

Now you can install nginx with a simple command:

yum install nginx

To start nginx and allow it to start at boot:, type:

service nginx start
chkconfig nginx on

If nginx doesn't start, make sure you haven't httpd service running and listening the same port (80). You can display running services with the command:

top

If you want to turn off httpd and disable it to start at boot, enter:

service httpd stop
chkconfig httpd off

Main config file is /etc/nginx/nginx.conf. Also you can add hosts and discribe them in additional conf files /etc/nginx/conf.d/*.conf. You can base on example config file /etc/nginx/conf.d/default.conf

How to install nginx on Debian.

First append nginx repo to /etc/apt/sources.list

deb http://nginx.org/packages/debian/ squeeze nginx
deb-src http://nginx.org/packages/debian/ squeeze nginx

Now install nginx

apt-get install nginx

Make sure you haven't apache2 service running and listening the same port (80). You can display running services with the command:

top

If you want to turn off apache2 and disable it to start at boot, enter:

service apache2 stop
update-rc.d -f apache2 remove

Service nginx is set to start at boot by default. To start it manually, enter:

service nginx start

Main config file is /etc/nginx/nginx.conf. Also you can add hosts and discribe them in additional conf files /etc/nginx/conf.d/*.conf.

How to install nginx on Ubuntu.

First append nginx repo to /etc/apt/sources.list

deb http://nginx.org/packages/ubuntu/ lucid nginx
deb-src http://nginx.org/packages/ubuntu/ lucid nginx

Now install nginx

apt-get install nginx

Make sure you haven't apache2 service running and listening the same port (80). You can display running services with the command:

top

If you want to turn off apache2 and disable it to start at boot, enter:

service apache2 stop
update-rc.d -f apache2 remove

Service nginx is set to start at boot by default. To start it manually, enter:

service nginx start

Main config file is /etc/nginx/nginx.conf. Also you can add hosts and discribe them in additional conf files /etc/nginx/conf.d/*.conf.

How to configure nginx server blocks

If you want to point several domains to one IP address you can use nginx server blocks.

Two Server Blocks, Serving Static Files

http {
  index index.html;

  server {
    server_name www.domain1.com;
    access_log logs/domain1.access.log main;

    root /var/www/domain1.com/htdocs;
  }

  server {
    server_name www.domain2.com;
    access_log  logs/domain2.access.log main;

    root /var/www/domain2.com/htdocs;
  }
}

A Default "Catch All" Server Block

http {
  index index.html;

  server {
    listen 80 default_server;
    server_name _; # This is just an invalid value which will never trigger on a real hostname.
    access_log logs/default.access.log main;

    server_name_in_redirect off;

    root  /var/www/default/htdocs;
  }
}

Wildcard Subdomains in a Parent Folder

 server {
  # Replace this port with the right one for your requirements
  listen 80 default_server;  #could also be 1.2.3.4:80

  # Multiple hostnames separated by spaces.  Replace these as well.
  server_name star.yourdomain.com *.yourdomain.com; # Alternately: _

  root /PATH/TO/WEBROOT;

  error_page 404 errors/404.html;
  access_log logs/star.yourdomain.com.access.log;

  index index.php index.html index.htm;

  # static file 404's aren't logged and expires header is set to maximum age
  location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
    access_log off;
    expires max;
  }

  location ~ \.php$ {
    include fastcgi_params;
    fastcgi_intercept_errors on;
    # By all means use a different server for the fcgi processes if you need to
    fastcgi_pass   127.0.0.1:YOURFCGIPORTHERE;
  }

  location ~ /\.ht {
    deny  all;
  }
}


Some troubleshooting

Problem: 403 Forbidden

Solution: For example if you have more than 1 domain at your nginx server and domain1 has links to domain2 which returns error 403 in browser, the problem could be in hotlinks. Open up your domain2 conf file and make sure it has domain1 in the list of valid referrers.

server {
            location ~* ^.+\.(gif|jpg|mpg|mp3|mpeg|avi)$ { 
               valid_referers none blocked www.domain1.com domain1.com;
               if ($invalid_referer) { 
                   return   403; 
               } 
               root $root_path; 
           }
        }

Source: official nginx wiki

Nginx reverse proxy example.

Nginx reverse proxy: http://wiki.vpsget.com/index.php/Nginx_Reverse_Proxy