天天看點

Nginx入門

1、Nginx簡介

Nginx

是一個輕量級高性能的HTTP和反向代理的Web伺服器。特點:占有記憶體少、并發能力強。

2、Nginx的安裝

Nginx官網

2.1、Windows下

下載下傳對應的版本、下載下傳後直接解壓

2.2、Linux下

(1)、安裝前準備

#安裝依賴:
sudo yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
           

(2)、下載下傳Nginx包,可官網下載下傳也可利用wget下載下傳

#cd 進入下載下傳包的路徑、這裡使用的/opt/soft
#在opt下建立soft
mkdir -p /opt/soft
# 進入到soft檔案夾
cd /opt/soft
           
Nginx入門
#下載下傳nginx安裝包
sudo wget http://nginx.org/download/nginx-1.18.0.tar.gz
# 如果提示sudo: wget:找不到指令,先安裝wget
sudo yum install wget
#解壓
sudo tar -zxvf nginx-1.18.0.tar.gz
           
Nginx入門
#進入nginx-1.18.0目錄
sudo cd nginx-1.18.0
#執行指令并編譯
sudo ./configure 
sudo make
sudo make install
           
Nginx入門

3、nginx的配置

cd /usr/local/nginx/conf/
           

預設配置檔案說明

#user  nobody;
#啟動程序,通常設定成和cpu的數量相等
worker_processes  1;

#工作模式及連接配接數上限
events {
	#單個背景worker process程序的最大并發連結數
    worker_connections  1024;
}

#設定http伺服器,利用它的反向代理功能提供負載均衡支援
http {
    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;
    
    #指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出檔案,對于普通應     #用,必須設為 on,如果用來進行下載下傳等應用磁盤IO重負載應用,可設定為 off,以平衡磁盤
    #與網絡#I/O處理速度,降低系統的uptime.
    sendfile        on;
    #tcp_nopush     on;

    #連接配接逾時時間
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    #開啟gzip壓縮
    #gzip  on;

	#伺服器相關配置
    server {
    	#偵聽80端口
        listen       80;
        
        #定義使用www.xx.com通路
        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;
    #    }
    #}

           

4、nginx常用指令

#進入到nginx的sbin目錄下
cd /usr/local/nginx/sbin

#指令

#啟動
./nginx

#重新加載配置檔案
./nginx -s reload

#檢視nginx程序
ps aux | grep nginx 檢視nginx程序

#停止(強制的)
./nginx -s stop

#安全退出(線程一個個關閉)
./nginx -s quit


           

5、檢視是否安裝成功

浏覽器輸入ip位址,出現以下界面表示安裝成功
           
Nginx入門

繼續閱讀