nginx安裝配置

1.安裝包下載http://nginx.org/en/download.htmlhtml

2.安裝下列一堆編譯工具及庫文件nginx

yum -y install make zlib zlib-devel gcc gcc-c++ libtool  openssl openssl-devel pcre pcre-develc++

安裝完成以下:瀏覽器

 安裝完成服務器

 啓動服務 命令--/usr/local/nginx/sbin/nginx 網絡

啓動成功app

在瀏覽器中訪問ip,默認配置是80端口,tcp

查看端口工具

 

須要先開放端口並使端口生效,性能

firewall-cmd --zone=public --add-port=80/tcp --permanent
systemctl restart firewalld.service

而後

 

 

 其配置詳解以下:考唄https://www.runoob.com/w3cnote/nginx-setup-intro.html

########### 每一個指令必須有分號結束。#################
#user administrator administrators;  #配置用戶或者組,默認爲nobody nobody。
#worker_processes 2;  #容許生成的進程數,默認爲1
#pid /nginx/pid/nginx.pid;   #指定nginx進程運行文件存放地址
error_log log/error.log debug;  #制定日誌路徑,級別。這個設置能夠放入全局塊,http塊,server塊,級別以此爲:debug|info|notice|warn|error|crit|alert|emerg
events {
    accept_mutex on;   #設置網路鏈接序列化,防止驚羣現象發生,默認爲on
    multi_accept on;  #設置一個進程是否同時接受多個網絡鏈接,默認爲off
    #use epoll;      #事件驅動模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
    worker_connections  1024;    #最大鏈接數,默認爲512
}
http {
    include       mime.types;   #文件擴展名與文件類型映射表
    default_type  application/octet-stream; #默認文件類型,默認爲text/plain
    #access_log off; #取消服務日誌    
    log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定義格式
    access_log log/access.log myFormat;  #combined爲日誌格式的默認值
    sendfile on;   #容許sendfile方式傳輸文件,默認爲off,能夠在http塊,server塊,location塊。
    sendfile_max_chunk 100k;  #每一個進程每次調用傳輸數量不能大於設定的值,默認爲0,即不設上限。
    keepalive_timeout 65;  #鏈接超時時間,默認爲75s,能夠在http,server,location塊。

    upstream mysvr {   
      server 127.0.0.1:7878;
      server 192.168.10.121:3333 backup;  #熱備
    }
    error_page 404 https://www.baidu.com; #錯誤頁
    server {
        keepalive_requests 120; #單鏈接請求上限次數。
        listen       4545;   #監聽端口
        server_name  127.0.0.1;   #監聽地址       
        location  ~*^.+$ {       #請求的url過濾,正則匹配,~爲區分大小寫,~*爲不區分大小寫。
           #root path;  #根目錄
           #index vv.txt;  #設置默認頁
           proxy_pass  http://mysvr;  #請求轉向mysvr 定義的服務器列表
           deny 127.0.0.1;  #拒絕的ip
           allow 172.18.5.54; #容許的ip           
        } 
    }
}

上面是nginx的基本配置,須要注意的有如下幾點:

一、幾個常見配置項:

  • 1.$remote_addr 與 $http_x_forwarded_for 用以記錄客戶端的ip地址;
  • 2.$remote_user :用來記錄客戶端用戶名稱;
  • 3.$time_local : 用來記錄訪問時間與時區;
  • 4.$request : 用來記錄請求的url與http協議;
  • 5.$status : 用來記錄請求狀態;成功是200;
  • 6.$body_bytes_s ent :記錄發送給客戶端文件主體內容大小;
  • 7.$http_referer :用來記錄從那個頁面連接訪問過來的;
  • 8.$http_user_agent :記錄客戶端瀏覽器的相關信息;

二、驚羣現象:一個網路鏈接到來,多個睡眠的進程被同事叫醒,但只有一個進程能得到連接,這樣會影響系統性能。

三、每一個指令必須有分號結束。

相關文章
相關標籤/搜索