CentOS7用yum安裝Nginx及使用


第一步:yum安裝nginx

1:添加源

sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

2:安裝nginx

sudo yum install -y nginx

出現Complete!代表安裝完成!

3:啟動nginx

systemctl start nginx.service

4:查看防火牆開啟的端口

firewall-cmd --zone=public --list-ports

我的為空,什么端口都沒開放,開放了的會有80/tcp

5:開放80端口(已開放80端口的就不用執行了)

firewall-cmd --zone=public --add-port=80/tcp --permanent

6:防火牆配置生效(已開放80端口的就不用執行了)

firewall-cmd --reload

7:再次查看開放的端口

firewall-cmd --zone=public --list-ports

有了,80/tcp

8:瀏覽器輸入ip訪問

訪問成功!

注:service訪問目錄如果在別的盤(如/www或/home),需要關閉selinux或者設置成寬容模式(setenforce 0),不然會報403錯誤

#查看當前狀態
systemctl status nginx.service
#啟動
systemctl start nginx.service
#停止
systemctl stop nginx.service
#重啟
systemctl restart nginx.service
#開機自啟動
systemctl enable nginx.service
#停止開機自啟動
systemctl disable nginx.service

9:nginx常用命令

#幫助
nginx -?或nginx -h

列出了可以使用的命令

#查看版本,小寫v
nginx -v
#查看版本以及編譯選項,大寫V
nginx -V

#檢查配置文件
nginx -t
#檢查配置文件並輸出配置
nginx -T

這個證明配置文件沒有語法問題,大寫T會在這下面把配置文件輸出,一般用不到

#靜默模式,檢查配置文件,非錯誤信息不顯示
nginx -q

#給nginx主進程發送信號,信號有stop、quit、reopen、reload
nginx
-s stop|quid|reopen|reload

stop:停止nginx服務

quit:停止nginx服務,但是需要worker進程完成現有的請求。就是不再接收新請求,然后現在已經接收的請求要處理完,然后再關閉

reopen:重新打開日志文件,可用於日志切割

reload:更改配置文件后可使用這個命令重新加載,執行過程:master進程接收到reload信號,會去重新加載解析新配置文件,如果新配置文件有錯誤,則繼續使用上次的配置文件運行並拋出錯誤,如果新配置文件正確,則向worker進程發出退出信號,worker進程就會停止接收新請求,空閑的woker會立刻關閉並被master進程重新創建,有正在處理請求的worker進程需要完成請求后再關閉,再這個過程中master主進程是不關閉的,所以客戶端的新請求也可以正常接收,下面運行一下試試

a:先看一下現在的nginx進程信息

b:更改nginx配置文件nginx.conf,將pid改成pidd,這樣配置文件就是錯誤的,改完后reload,會輸出錯誤信息

c:查看nginx進程信息,發現master和worker進程並沒有發生變化,證明並沒有重載,還是和執行reload前一樣

d:將nginx.conf改回來並reload,沒有錯誤輸出說明成功了,再次查看nginx進程,發現master進程的pid沒變,但是worker進程的pid變了

 

#設置前綴路徑
nginx -p prefix

 

#指定錯誤日志路徑,默認/var/log/nginx/error.log
nginx -e filename

 

#指定配置文件路徑,默認/etc/nginx/nginx.conf
nginx -c filename

 

#指定全局指定配置文件
nginx -g directives

 10:配置文件

  配置文件是根目錄下的nginx.conf,內容可分為main全局段、events段、http段、server段、upstream段、location段;下面大概介紹一下每個段主要配置的內容

  【main全局段】 

    介紹:用於配置用戶,進程,錯誤日志等相關參數

    常用參數:

      user nginx;#worker進程身份

      worker_processes 4;#worker進程數量,一般與cpu核心數相等(lscpu可查看cpu信息),建議不超過cpu核心的2倍,默認auto(會自動去匹配cup核心數)

      worker_cpu_affinity 1000 0100 0010 0001;#worker進程綁定CPU核心,cpu是幾核就用幾個0代表(對應位置0代表不使用,1代表使用),幾個worker進程就寫幾個(用空格隔開)

      error_log filename;#全局錯誤日志

      pid nginx.pid;#指定pid文件(用來存放nginx主控進程的進程號)

  【events段】

    介紹:用於配置IO模型(如epoll、kqueue、select或poll)、work進程連接數等

    常用參數:

      #事件驅動

      use epoll;

      #每個worker進程的最大連接數,默認最大是1024,可以根據cpu的使用程度來具體調整,同時操作系統的“進程最大可打開文件數”也會限制最大值。

      #每個請求會占用worker的2個或4個連接數,靜態訪問占用2個,http請求占用4個

      #所以支持的最大並發數也就是【靜態worker_connections * worker_processes /2】【http請求worker_connections * worker_processes /4】。

      worker_connections 1024;

  【http段】

    介紹:http相關模塊支持,內含server、upstream

    常用參數:

      #nginx支持的媒體類型庫文件

      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  /var/log/nginx/access.log  main;#main就是我們上面定義的日志格式名稱

      #超時時間

      keepalive_timeout 60;

      #默認編碼

      charset utf-8;

      #設定通過nginx上傳文件的大小

      client_max_body_size 100m;

      #隱藏nginx版本號

      server_tokens off;

 

      #開啟高效文件傳輸模式(也可設置在server里)

      sendfile on;

      tcp_nopush on;

      tcp_nodelay on;#on:發送報文不延時不管數據包多小都及時發送;off:會等到一定量的數據報文一起發送

 

‌      #gzip壓縮優化,訪問加快,消耗cpu多,純文本壓縮率高,但要大於1kb,不然可能越壓越大,圖片視頻壓縮率低,也可能越壓越大(也可設置在server里)

      gzip on;#開啟壓縮

      gzip_min_length 1k;#壓縮的頁面最小字節

      gzip_buffers 432k;#壓縮緩存區大小

      gzip_http_version 1.1;#壓縮版本

      gzip_comp_level 9;#壓縮比率

      gzip_types text/css text/xml application/javascript;#指定壓縮的類型

  【server段】

    介紹:配置虛擬主機,包含location段

    參數:
      #監聽端口

      listen 80;#443 ssl

      #域名,可多個,用空格隔開

      server_name www.aaa.com

      #訪問目錄

      root /www

      #默認起始頁

      index index.php index.html

      #設置https     

      ssl on;#啟用ssl功能

      ssl_certificate /etc/nginx/ssl/admin.zkzgh.com/admin.zkzgh.com.pem;#證書

      ssl_certificate_key /etc/nginx/ssl/admin.zkzgh.com/admin.zkzgh.com.key;#證書私鑰文件

      ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;

      ssl_protocols TLSv1 TLSv1.1 TLSv1.2;#支持的ssl協議版本

      ssl_session_timeout 5m;#客戶端一側的連接可以復用ssl session cache中緩存 的ssl參數的有效時長;

      ssl_prefer_server_ciphers on;

      #http轉https    

      server {

        listen 80;

        server_name www.aaa.com;

        rewrite ^(.*) https://www.aaa.com$1 permanent;

      }

  【upstream段】

    介紹:只能用於http配置段中,意思是定義一組后端服務器組

    示例:

      upstream name {

        server www.a.com;

        server www.b.com weight=2;#weight是權重,默認是1

      }

  【location段】

    介紹:url匹配到實際,優先級(=, ^~, ~/~*,不帶符號)

    #示例:

      # 不存在的文件轉發到index.html

      location / {

        if (!-e $request_filename) {
          rewrite ^(.*)$ /index.php$1 last;
          break;
        }
      }

      #設置錯誤頁面    

      error_page 500 502 503 504 /50x.html;

      location = /50x.html {

        root html;

      }

      #php請求發送到phpfpm   

      location ~ \.php(.*)$ {

        fastcgi_pass 127.0.0.1:9000;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        fastcgi_param PATH_INFO $1;

        include fastcgi_params;

        client_max_body_size 20m;

        client_body_temp_path /tmp/nginx_tmp;

      }

      #轉發“域名/api/url”到“http://api”,可以和upstream段聯合使用

      location ^~/api {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_buffering off;
        rewrite ^/api/(.*)$ /$1 break;
        proxy_pass http://api;
      }

      #upstream api {

        #server http://www.api.com;
      #}

      #查看nginx狀態

      location /status {

        stub_status on;

        access_log off;

        allow 192.168.100.0/24;

        deny all;

      }

      #防盜鏈

      location ~* \.(rmvb|jpg|png|swf|flv)$ {

        #上面后綴的文件實行防盜鏈

        valid_referers none blocked www.a.com;

        #表示對www.a.com此域名開通白名單,多個用空格隔開

        if ($invalid_referer) {

          #如果請求不是從www.a.com白名單發出來的請求,直接重定向到403.html這個頁面或者返回403

          #rewrite ^/ 404.jpg;

          return 403;

        }

      }

      #防爬蟲

      if ($http_user_agent ~* LWP:Simple|BBBike|wget) {

        return 403 ;

        rewrite ^(.*) http://www.a.com/$1 permanent;

      }

 

      #禁止ip訪問

      location / {

        allow 202.111.12.211;

        deny all;

      }

      #不記錄不需要的日志

      location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$ {

        access_log off;

      }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM