天天看點

nginx介紹和安裝

一、nginx特性介紹

nginx優點

nginx是一個高性能web的反向代理伺服器,Apache擅長處理動态資源,nginx擅長處理靜态資源。

1.在高并發的情況下nginx是Apache伺服器不錯替代品能支援50000個并發連接配接數相應。

2.使用epoll and kqueue作為開發模型

3.作為負載均衡伺服器:nginx可支援PHP程式對外進行服務,也支援HTTP代理伺服器對外進行服務。

4nginx消耗CPU資源小,3萬并發連接配接下10個nginx一共隻消耗150M記憶體。

5.配置簡單成本低廉,支援Rewrite重寫規則

6.内置的健康檢查功能:背景某台伺服器出問題了,不會影響前台通路

7.節省帶寬穩定性高、支援動态編譯

8.外圍開發好子產品多,可以不停重載配置檔案

Nginx 的擴充功能

基于名稱和IP的虛拟主機

支援keepalive

支援平滑更新

定制通路日志,支援使用日志緩沖區提高日志存儲性能

支援URL重寫

支援路徑别名

支援基于IP及使用者的通路控制

支援速率限制,支援并發數限制

Nginx 的應用類别

使用nginx結合FastCGI運作的PHP、 JSP 、Perl等程式

使用nginx作反代理、負載均衡、過濾規則

使用nginx運作靜态HTML網頁、圖檔

nginx與其他新技術的結合應用

nginx的子產品分類

nginx的子產品從結構上分為核心子產品、基礎子產品和第三方子產品

HTTP子產品、EVENT子產品和MAIL子產品等屬于核心子產品

HTTP Access子產品、HTTP FastCGI子產品、HTTP Proxy子產品和HTTP Rewrite子產品屬于基本子產品

HTTP Upstream子產品、Request Hash子產品、Notice子產品和HTTP Access Key子產品屬于第三方子產品

二、安裝nginx

01、關閉防火牆和setLinux

[[email protected] ~]# setenforce 0                                                                 \\關閉setlinux
[[email protected] ~]# systemctl stop firewalld                                             \\關閉防火牆
[[email protected] ~]#yum -y install wget vim
           

02、安裝nginx軟體建立環境

[[email protected] ~]# useradd -r -M -s /sbin/nologin nginx                        \\建立使用者
[[email protected] ~]# mkdir -p /var/log/nginx                                      \\建立檔案夾
[[email protected] ~]# chown -R nginx.nginx /var/log/nginx                         \\授權檔案夾                
[[email protected] ~]# cd /usr/src/
[[email protected] ~]#yum -y install wget
[[email protected] src]# wget http://nginx.org/download/nginx-1.12.0.tar.gz
[[email protected] src]#
[[email protected] ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++     
[[email protected] src]# tar -xf nginx-1.12.0.tar.gz                                           \\解壓
[[email protected] src]# ls
debug  kernels  nginx-1.12.0  nginx-1.12.0.tar.gz
[[email protected] src]# cd nginx-1.12.0
[[email protected] nginx-1.12.0]# ./configure \                                                   \\編譯安裝
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[[email protected] nginx-1.12.0]# make -j $(grep 'processor' /proc/cpuinfo | wc-l) && make install
           

三、對nginx進行配置

[[email protected] ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh  \\環境配置
[[email protected] nginx-1.12.0]# . /etc/profile.d/nginx.sh     
[[email protected] ~]# nginx -c /usr/local/nginx/conf/nginx.conf                \\可指定讀取的檔案(此項也可以不輸入)
[[email protected] nginx-1.12.0]# nginx
[[email protected] nginx-1.12.0]# ss -antl
State       Recv-Q Send-Q                                                           Local Address:Port                                                                          Peer Address:Port              
LISTEN      0      128                                                                          *:80                                                                                       *:*                  
LISTEN      0      128                                                                          *:22                                                                                       *:*                  
LISTEN      0      100                                                                  127.0.0.1:25                                                                                       *:*                  
LISTEN      0      128                                                                         :::22                                                                                      :::*                  
LISTEN      0      100                                                                        ::1:25                                                                                      :::* 
           

在浏覽器輸入自己的ip:192.168.69.134顯示如圖成功

nginx介紹和安裝
[[email protected] ~]# cd /usr/local/nginx/html/              
[[email protected] html]# vim a.html                         \\嘗試建立html檔案
hello goodbbye!!
在浏覽器中輸入:192.168.69.134/a.html,成功讀取
![在這裡插入圖檔描述](https://img-blog.csdn.net/20181018160930391?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjgzNzYzNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
           

四、關于nginx的一些配置設定

第一種在關于通路html的配置

大家注意如果需要把浏覽檔案放在某一個檔案夾下

第一必須在/usr/local/nginx/html/目錄下建立

第二必須第一修改配置在,舉例子例如建立一個檔案夾名字叫xiaoming

[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
        location / {
            root   html;
            index  index.html index.htm;
        }
       
        location /xiaoming {                                              \\以下為增加項目
            root   html;
            index  index.html index.htm;
        }
           

第三我們建立目錄和檔案

[[email protected] html]# mkdir xiaoming
[[email protected] html]# cd xiaoming
[[email protected] xiaoming]# b.html
[[email protected] xiaoming]# vim b.htm
hello wo shi xiaoming
[[email protected] xiaoming]# nginx -s reload      \\重新加載nginx
           

在浏覽器輸入192.168.69.134/xiaoming/b.html

顯示如下驗證成功

nginx介紹和安裝