nginx安裝與配置

編譯安裝nginx

1.安裝Nginx所需pcre庫

????pcre全程Perl Compatible Regular Expressions,中文perl兼容正則表達式,官方網址:http://www.pcre.org/,安裝pcre庫是為了使Nginx支持HTTP Rewrite模塊。安裝過程如下:

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
tar xzvf pcre-8.40.tar.gz
cd pcre-8.40/
./configure
make && make install

2.編譯openssl

wget https://www.openssl.org/source/openssl-1.0.2l.tar.gz
tar xzf openssl-1.0.2l.tar.gz
cd openssl-1.0.2l
./config
make && make install

3.安裝Nginx

wget http://nginx.org/download/nginx-1.12.1.tar.gz
# 添加nginx用戶,-s /sbin/nologin 禁止登錄 -M 不產生home目錄
useradd nginx -s /sbin/nologi -M
tar xzvf nginx-1.12.1.tar.gz
cd nginx-1.12.1
./configure --user=nginx --group=nginx --prefix=/application/nginx-1.12.1 --with-http_stub_status_module --with-http_ssl_module --with-openssl=/usr/local/src/openssl-1.0.2l(若編譯出現ssl相關錯誤,加上此選項重新編譯,此目錄是openssl的源碼目錄,可根據實際情況自行修改)
make && make install
ln -s /application/nginx-1.12.1 /application/nginx
# 檢查nginx配置是否有錯誤
/application/nginx/sbin/nginx -t
# 若出現找不到某些共享庫的情況,通過find找到該庫的路徑目錄,然后加入到/etc/ld.so.conf,然后執行ldconfig命令使其生效
# 若語法檢查沒有錯誤,可以直接啟動nginx
/application/nginx/sbin/nginx
/application/nginx/sbin/nginx -s reload # 平滑重啟

4.排查問題

????此時訪問80端口,會出現Welcome to Nginx的頁面,若沒有成功,可以從以下幾個方面檢查:

4.1.iptables防火墻和selinux是否關閉。

# 關閉防火墻
/etc/init.d/iptables stop
# 若允許外網ip訪問80端口,則不能關閉,而是將防火墻80端口放開,命令:
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
# 非正式環境可以禁止防火墻開啟啟動,便于學習調試Nginx服務,具體請學習防火墻技術
chkconfig iptables off
# 查看防火墻狀態
/etc/init.d/iptables status

4.2.關閉selinux

# 臨時關閉selinux
setenforce 0
# 永久關閉,編輯/etc/selinux/config,配置如下選項,并重啟生效
SELINUX=disable

4.3.檢查日志

nginx默認日志文件存放在啟動目錄的logs文件夾下,查看日志獲取報錯信息,解決問題
windows中C:\Windows\System32\drivers\etc\hosts用于電腦解析DNS配置

二.Nginx配置

1.Nginx的配置文件

????nginx配置文件放置在安裝目錄的conf目錄下,其中nginx.conf為其主要配置文件,相當于apache的httpd.conf文件,配置文件的每行都以;結尾。

user nginx nginx; # 配置nginx服務的用戶及組
worker_processes  1; # 配置啟動進程的個數,一般為cpu個數的2倍
error_log /application/logs/nginx_error.log crit; # 錯誤日志的配置及日志級別
pid   logs/nginx.pid; # 配置存放nginx進程ID的文件,方便后續發送信號
events {
    use epoll; # 使用epoll
    worker_connections 1024;
}
http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;
    # 日志格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        '$status $body_bytes_sent "$http_referer" '
        '"$http_user_agent" "$http_x_forwarded_for"';

    server { # 需要包含在http {} 中用于虛擬主機的配置
        listen 80;
        server_name www.flyingdown.com flyingdown.com; # 配置域名及別名
        location / {
            root html; # 配置該虛擬主機默認站點目錄,本例相當于/application/nginx/html目錄
            index index.html index.htm; # 配置默認頁面
            # 配置訪問日志
            access_log /application/logs/www_access.log main
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
    }
}

2.優化vhosts配置

????主配置文件可以包含虛擬主機配置,但當虛擬主機很多時,會使主配置文件看起來很亂,不便于維護,我們可以使用include語法,將虛擬主機分離出來。

????主配置文件

user nginx nginx;
worker_processes  2;
error_log /app/logs/nginx_err.log crit;
events {
    use epoll;
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

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

    include extra/nginx_vhosts.conf;
}

????然后新建extra目錄,新建nginx_vhosts.conf,寫入一下內容

server {
    listen       80;
    server_name  www.flydowning.com flydowning.com;
    location / {
        root   html/base;
        index  index.html index.htm;
    access_log /app/logs/www_access.log main;
    }

    location ~.*/static/ {
        root   html;
    access_log /app/logs/couplet_access.log main;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

server {
    listen       80;
    server_name  couplet.flydowning.com;
    location / {
        root   html/couplet;
        index  index.html;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

????甚至,我們可以將每一個虛擬主機的配置放置一個單獨的文件,然后使用include語法,包含至主配置文件中,這里不做展示

3.配置狀態信息虛擬主機

????如果我們想在nginx運行時動態的觀察nginx運行的狀態、連接數等信息,我們可以添加狀態信息虛擬主機的配置,如下:

server {
    listen 80;
    server_name status.flyingdown.com;
    location / {
        stub_status on;
        access_log off;
    }
}

????在hosts文件中添加status.flyingdown.com的映射,然后訪問,可以看到如下內容:

????

4.基于端口的虛擬主機配置

????修改虛擬主機的listen端口即可

5.基于IP的虛擬主機配置

????將server_name修改為對應的IP地址,同時將listen監聽添加上ip:port形式

6.配置多個nginx實例

????Nginx多實例命令參數,我們可以通過安裝后的/application/nginx/sbin/nginx加上-h參數查看相關用法:

[-root-@/root]#/application/nginx/sbin/nginx -h
nginx version: nginx/1.12.1
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
    -?,-h         : this help
    -v            : show version and exit
    -V            : show version and configure options then exit
    -t            : test configuration and exit
    -T            : test configuration, dump it and exit
    -q            : suppress non-error messages during configuration testing
    -s signal     : send signal to a master process: stop, quit, reopen, reload
    -p prefix     : set prefix path (default: /application/nginx-1.12.1/)
    -c filename   : set configuration file (default: conf/nginx.conf)
    -g directives : set global directives out of configuration file

????可以使用-c參數指定其他配置文件啟動多個nginx實例

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容