Ubuntu16.04安装及配置nginx

Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。

首先需安装nginx依赖库

1.安装gcc g++的依赖库

apt-get install build-essential
apt-get install libtool

2.安装pcre依赖库

sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev

3.安装zlib依赖库

apt-get install zlib1g-dev

4.安装ssl依赖库

apt-get install openssl

安装nginx

复制代码
#下载最新版本:
wget http://nginx.org/download/nginx-1.11.3.tar.gz
#解压:
tar -zxvf nginx-1.11.3.tar.gz
#进入解压目录:
cd nginx-1.11.3
#配置:
./configure --prefix=/usr/local/nginx 
#编辑nginx:
make
注意:这里可能会报错,提示“pcre.h No such file or directory”,具体详见:http://stackoverflow.com/questions/22555561/error-building-fatal-error-pcre-h-no-such-file-or-directory
需要安装 libpcre3-dev,命令为:sudo apt-get install libpcre3-dev
#安装nginx:
sudo make install
#启动nginx:
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
注意:-c 指定配置文件的路径,不加的话,nginx会自动加载默认路径的配置文件,可以通过 -h查看帮助命令。
#查看nginx进程:
ps -ef|grep nginx
复制代码

配置nginx

cd /usr/local/nginx/conf/

使用vim或nano编辑器在该目录下新建一个hgame.conf文件输入以下内容:

#这里的hgame和proxy_pass是对应的关系,如果不listen 80端口的话,比如listen端口是8011,http://www.hgame.wang:8011/text

复制代码
upstream hgame{
    server 127.0.0.1:9001; #Tornado
}
server {
    listen 80;
    server_name  www.hgame.com hgame.com;
    #root   html;
    #index  index.html index.htm index.py index;
    ## send request back to Tornado ##
    location / {
        proxy_pass  http://hgame;
        #Proxy Settings
        proxy_redirect     off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;
        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
   }
}
## End www.ihasy.com ##
复制代码

再使用vim或nano打开 /usr/local/nginx/conf/nginx.conf

nano /usr/local/nginx/conf/nginx.conf

在http下添加一行

include hagme.conf

保存,重启nginx,即可实现反向代理。

重启命令

# /usr/local/nginx/sbin/nginx -s reload 

  kill -HUP pid

 
pid 是进程标识。如果想要更改配置而不需停止并重新启动服务,请使用该命令。在对配置文件作必要的更改后,发出该命令以动态更新服务配置。

根据约定,当您发送一个挂起信号(信号 1 或 HUP)时,大多数服务器进程(所有常用的进程)都会进行复位操作并重新加载它们的配置文件。清单 2 显示了向所有正在运行的 Web 服务器进程发送挂起信号的一种方法。


清单 2. 告诉 Web 服务器重新加载其配置文件并对文件进行复位操作
root@holy [507]$ ps -A | grep httpd | grep -v grep | awk '{ print $1; }' | xargs -L 1 sudo kill -HUP  
上面的命令中包含了很多的操作,所以让我们来仔细地看一下管道的每个部分。ps 和 grep 命令用来在所有的进程中搜索 httpd(并且忽略用来搜索 httpd 进程的 grep 进程)。接下来,awk 只显示输出结果中的进程 ID,并将它传递给 xargs。然后,xargs 命令接受每个进程 ID(因为使用了 -L 1 以便一次提取一行内容),并使用 sudo kill -HUP 向相应的进程发送一个挂起信号。

LINUX和Unix都适用: 
改/etc/ssh/sshd_config,将里面的Port改为新端口,比如10022,然后 kill -HUP `cat /var/run/sshd.pid` 就行了。 
现有连接自己不会断,因为kill -HUP `cat /var/run/sshd.pid` 只是HUP监听的那个,已经建立的连接(不同的 pid)不会断。 

然后你在现有账号下面exit,在用新的端口登陆就可以了。 

PS:

HUP(1)是让进程挂起,睡眠;

kill (9)六亲不认的杀掉

term(15)正常的退出进程

因为进程可能屏蔽某些信号,所以它们的用处也就不一样。。。

 

多个配置:

upstream fruit1{
    server 127.0.0.1:18082; #Tornado
}
server {
    listen 80;
    server_name  a.hgame.wang;
    location / {
        proxy_pass  http://fruit1;
        proxy_redirect     off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;
        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
   }
}
upstream fruit2{
    server 127.0.0.1:18083; #Tornado
}
server {
    listen 80;
    server_name  b.hgame.wang;
    location / {
        proxy_pass  http://fruit2;
        proxy_redirect     off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;
        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
   }
}

 

 

server {
    listen 80;
    server_name  c.hgame.wang;
    location / {
        root  /data/www/h5/html5-fruit-ninja;
        autoindex on;
    } 
}
server  {
    listen 80;
    server_name  c.hgame.wang;
    location /git {
        root  /data/www/h5/html5-fruit-ninja;
        autoindex on;
    } 
}

 

整体的配置:

nginx.conf:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include fruit.conf;
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8088;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

 

 

 

fruit.conf:

upstream fruit1{
    server 127.0.0.1:18082; #Tornado
}
server {
    listen 80;
    server_name  a.hgame.wang;
    location / {
        proxy_pass  http://fruit1;
        proxy_redirect     off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;
        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
   }
}
upstream fruit2{
    server 127.0.0.1:18083; #Tornado
}
server {
    listen 80;
    server_name  b.hgame.wang;
    location / {
        proxy_pass  http://fruit2;
        proxy_redirect     off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;
        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
   }
}
server {
    listen 80;
    server_name  c.hgame.wang;
    location / {
        root  /data/www/h5/;
        autoindex on;
    }
}

server {
    listen 80;
    server_name  hgame.wang;
    location / {
        root  /data/www/h5/html5-fruit-ninja/;
        autoindex on;
    }
}


server {
    listen 80;
    server_name www.hgame.wang;
    location / {
        root  /data/www/h5/html5-fruit-ninja/;
        autoindex on;
    }
}

 

posted @ 2019-11-13 14:53  天涯过者  阅读(1310)  评论(0编辑  收藏  举报