http://nginx.org/keys/nginx_signing.key

$ sudo wget http://nginx.org/keys/nginx_signing.key
$ sudo apt-key add nginx_signing.key

2.在 /etc/apt/sources.list 文件中加入下面两行:

deb http://nginx.org/packages/ubuntu/ codename nginx
deb-src http://nginx.org/packages/ubuntu/ codename nginx
Ubuntu 下安装 Nginx_1.12.0及简单使用

3.执行更新安装:

$ apt-get update
$ apt-get install nginx

出现错误:dpkg: error processing archive /var/cache/apt/archives/nginx_1.12.0-1~precise_amd64.deb
解决办法:

$ sudo dpkg -i --force-overwrite /var/cache/apt/archives/nginx_1.12.0-1~precise_amd64.deb
$ sudo apt-get -f install nginx
$ nginx -v
nginx version: nginx/1.12.0

Beginner's Guide

nginx is read as 'engine x'.
By default, the configuration file is named nginx.conf and placed in the directory /usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx.

Starting, Stopping, and Reloading Configuration

$ service nginx start
$ nginx -s signal

Where signal may be one of the following:

  • stop -- fast shutdown
  • quit -- graceful shutdown
  • reload -- reloading the configuration file
  • reopen -- reopening the log files

The process ID of the nginx master process is written, by default, to the nginx.pid in the directory /usr/local/nginx/logs or /var/run.

$ kill -s QUIT 1628
$ ps -ax | grep nginx    # get the list of all running nginx processes

Configuration File's Structure

Example Configuration:

user www www;
worker_processes 2;

error_log /var/log/nginx-error.log info;

events {
    use kqueue;
    worker_connections 2048;    #this is a comment.
}

Serving Static Content

http {
    server {
        location / {
            root /data/www;
        }

        location /images/ {
            root /data;
        }
    }
}

In case something does not work as expected, you may try to find out the reason in acces.log and error.log files in the directory /usr/local/nginx/logs or /var/log/nginx.

Setting Up a Simple Proxy Server

server {
    location / {
        proxy_pass http://localhost:8080/;
    }

    location ~ \.(gif|jpg|png)$ {    # A regular expression should be preceded with ~
        root /data/images
    }
}

Setting Up FastCGI Proxying

server {
    location / {
        fastcgi_pass localhost:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param QUERY_STRING $query_string;
    }

    location ~ \.(gif|jpg|png)$ {
        root /data/images;
    }
}