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编辑器在该目录下新建一个ihasy.conf文件输入以下内容:

    server 127.0.0.1:9001; #Tornado
}
server { listen 80; server_name www.ihasy.com ihasy.com; #root html; #index index.html index.htm index.py index; ## send request back to Tornado ## location / { proxy_pass http://ihasy; #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; } }

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

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

在http下添加一行

include ihasy.conf

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

sudo /usr/local/nginx/sbin/nginx -t

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

 nginx多个转发的配置

upstream u1{
    server 127.0.0.1:18082; #Tornado
}

server {
    server_name  a.kejiefu.top;

    #root   html;
    #index  index.html index.htm index.py index;

    ## send request back to Tornado ##
    location / {
        proxy_pass  http://u1;

        #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;
   }
}
upstream u2{ server
127.0.0.1:8088; #Tornado } server { server_name www.kejiefu.top; #root html; #index index.html index.htm index.py index; ## send request back to Tornado ## location / { proxy_pass http://u2; #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; } }

 

posted @ 2019-02-15 17:26  天涯过者  阅读(229)  评论(0编辑  收藏  举报