Nginx的配置與開發學習(一):服務器安裝與配置

確認工作

  • ping www.baidu.com
  • 確認yum源是否可用 yum list|grep gcc
  • 關閉iptables配置 iptables具體用法
  • iptables -L(查看是否有iptables規則)
  • iptables -F
  • iptables -t nat -L
  • iptables -t nat -F
  • 關閉SELinex
  • getenforce 查看是否關閉 沒關閉的話就 setenforce 0
  • 安裝包 yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake
  • 安裝工具包 yum -y install wget httpd-tools vim
  • 到相關目錄 cd /opt/ 建立文件下mkdir XX XX XX XX
    所以刪除文件夾是 使用rm -rf 目錄名字
    -r 就是向下遞歸,不管有多少級目錄,一併刪除
    -f 就是直接強行刪除,不作任何提示的意思

Nginx簡述

Nginx是一個開源且高性能,可靠的HTTP中間件,代理服務

Nginx優勢

  1. IO多路複用epoll(老師出題給多個學生做,老師一個個去問或者許多老師監控每一個學生不如 學生主動上報)

​ 多個描述符的I/O操作都能在一個線程內併發交替地順序完成,這就叫I/O多路複用,複用指的是複用同一個線程。

  1. 輕量級 足夠輕量級的Web服務

    功能模塊少 只放核心代碼,插件代碼不會放 因此捨棄功能而注重性能

    代碼模塊化 易讀,可以進行二次的改進

  2. CPU親和(affinity)

    是一種把CPU核心和Nginx工作進程綁定方式,把每一個worker進程固定在一個cpu上執行,減少切換cpu的cache miss,獲得更好的性能。

  3. sendfile

    file要經過內核—>用戶空間–>通信,sendfile只通過內核–>通信了

Nginx安裝

  1. 登錄網站

  2. Pre-Built Packages for Stable version
    To set up the yum repository for RHEL/CentOS, create the file named /etc/yum.repos.d/nginx.repo with the following contents:
    
    [nginx]
    name=nginx repo
    baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
    gpgcheck=0
    enabled=1
    
    Replace “OS” with “rhel” or “centos”, depending on the distribution used, and “OSRELEASE” with “6” or “7”, for 6.x or 7.x versions, respectively.
    
    上面的註解特別重要所以實際上是在vim /etc/yum.repos.d/nginx.repo
    [nginx]
    name=nginx repo
    baseurl=http://nginx.org/packages/centos/7/$basearch/
    gpgcheck=0
    enabled=1
    
  3. yum list|grep nginx 查看

  4. yum install nginx 安裝nginx

  5. nginx -v 查看nginx版本號

安裝參數

命令: rpm -ql nginx 在操作系統上的相關配置文件

MIME(Multipurpose Internet Mail Extension,多用途因特網郵件拓展)

路徑 類型 作用
/etc/logrotate.d/nginx 配置文件 Nginx日誌輪轉,用於logrotate服務的日誌切割
/etc/nginx,
/etc/nginx/nginx.conf,
/etc/nginx/conf.d,
/etc/nginx/conf.d/default.conf
目錄,配置文件 Nginx主配置文件
/etc/nginx/fastcgi_params,
/etc/nginx/uwscgi_params,
/etc/nginx/scgi_params
配置文件 cgi配置相關,fastcgi
/etc/nginx/koi-utf,
/etc/nginx/koi-win,
/etc/nginx/win-utf
配置文件 編碼轉換映射轉化文件
/etc/nginx/mime.types 配置文件 設置http協議的Content-Type與拓展名對應關係
/usr/lib/systemd/system/nginx-debug.service,
/usr/lib/systemd/system/nginx.service,
/etc/sysconfig/nginx,
/etc/sysconfig/nginx-debug
配置文件 用於配置出系統守護進程管理器管理方式
/usr/lib64/nginx/modules,
/etc/nginx/modules
目錄 Nginx模塊目錄
/usr/sbin/nginx,
/usr/sbin/nginx-debug
命令 Nginx服務的啓動管理的終端命令

| /usr/share/doc/nginx-1.15.5
/usr/share/doc/nginx-1.15.5/COPYRIGHT
/usr/share/man/man8/nginx.8.gz | 文件,目錄 | Nginx的手冊和幫助文件 |
| /var/cache/nginx | 目錄 | Nginx的緩存目錄 |
| /var/log/nginx | 目錄 | Nginx的日誌目錄 |

命令: nginx -V 顯示安裝編譯參數

編譯選項 作用
–prefix=/etc/nginx
–sbin-path=/usr/sbin/nginx
–modules-path=/usr/lib64/nginx/modules
–conf-path=/etc/nginx/nginx.conf
–error-log-path=/var/log/nginx/error.log
–http-log-path=/var/log/nginx/access.log
–pid-path=/var/run/nginx.pid
–lock-path=/var/run/nginx.lock
安裝目的目錄或路徑
–http-client-body-temp-path=/var/cache/nginx/client_temp
–http-proxy-temp-path=/var/cache/nginx/proxy_temp
–http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
–http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp
–http-scgi-temp-path=/var/cache/nginx/scgi_temp
執行對應模塊時,Nginx所保留的臨時性文件(臨時性文件,不重要的文件)
–user=nginx
–group=nginx
設置Nginx進程啓動的用戶和組用戶
–with-cc-opt=parameters 設置額外的參數將被添加到CFLAGS變量

Centos操作

  1. cd /etc/nginx/ 到nginx目錄下

  2. vim nginx.conf

    user  nginx;    //設置nginx服務的系統使用用戶
    worker_processes  1;   //工作進程數,跟CPU數保持一致
    
    error_log  /var/log/nginx/error.log warn;  //nginx的錯誤日誌  日誌級別
    pid        /var/run/nginx.pid;            //nginx服務啓動時候pid
    
    
    events {
        worker_connections  1024;  //每個進程允許最大連接數
    }
    
    
    http {
        include       /etc/nginx/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  /var/log/nginx/access.log  main;  //main表示main的格式進入access.log
    
        sendfile        on;
        #tcp_nopush     on;
    
        keepalive_timeout  65;
    
        #gzip  on;
    
        include /etc/nginx/conf.d/*.conf;
    }
    
    
  3. cat conf.d/default.conf

    server {
        listen       80;  //確保端口不被佔用
        server_name  localhost;
    
        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;
    
        location / {
            root   /usr/share/nginx/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   /usr/share/nginx/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;
        #}
    }
    
    
    1. 重啓nginx systemctl restart nginx.service

Nginx變量

  1. HTTP請求變量-arg_PARAMETER,http_HEADER,sent_http_HEADER
  2. 內置變量 -Nginx內置的
  3. 自定義變量-自己定義 更多參數配置可以看一下 [Nginx官網](
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章