Ubuntu16.04安裝及配置nginx

Nginx ("engine x") 是一個高性能的 HTTP 和 反向代理 服務器,也是一個 IMAP/POP3/SMTP 代理服務器。 Nginx 是由 Igor Sysoev 爲俄羅斯訪問量第二的 Rambler.ru 站點開發的,第一個公開版本0.1.0發佈於2004年10月4日。其將源代碼以類BSD許可證的形式發佈,因它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而聞名。html

首先需安裝nginx依賴庫nginx

1.安裝gcc g++的依賴庫vim

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

安裝nginxui

複製代碼
#下載最新版本:
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
複製代碼

配置nginxspa

cd /usr/local/nginx/conf/

使用vim或nano編輯器在該目錄下新建一個ihasy.conf文件輸入如下內容:代理

複製代碼
upstream ihasy  {
    server 127.0.0.1:9001; #Tornado
}

## Start www.ihasy.com ##
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;
   }
}
## End www.ihasy.com ##
複製代碼

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

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

在http下添加一行

include ihasy.conf

保存,重啓nginx,便可實現反向代理。

nginx基本操做,啓動、重啓

啓動

 啓動代碼格式:nginx安裝目錄地址 -c nginx配置文件地址

例如:

[root@LinuxServer sbin]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

中止

 nginx的中止有三種方式:

  • 從容中止

  一、查看進程號

[root@LinuxServer ~]# ps -ef|grep nginx

  二、殺死進程

[root@LinuxServer ~]# kill -QUIT 2072

  • 快速中止

一、查看進程號

[root@LinuxServer ~]# ps -ef|grep nginx

二、殺死進程

[root@LinuxServer ~]# kill -TERM 2132
或 [root@LinuxServer ~]# kill -INT 2132

  • 強制中止

[root@LinuxServer ~]# pkill -9 nginx

重啓

一、驗證nginx配置文件是否正確

方法一:進入nginx安裝目錄sbin下,輸入命令./nginx -t

看到以下顯示nginx.conf syntax is ok

nginx.conf test is successful

說明配置文件正確!

方法二:在啓動命令-c前加-t

 二、重啓Nginx服務

 方法一:進入nginx可執行目錄sbin下,輸入命令./nginx -s reload 便可

方法二:查找當前nginx進程號,而後輸入命令:kill -HUP 進程號 實現重啓nginx服務

相關文章
相關標籤/搜索