Nginx的安裝與配置

Nginx是一款提供http、反向代理以及IMAP/POP3/SMTP的WEB服務器,它有着開源、免費、高性能及高併發等特色,近幾年陸續超過微軟的IIS和Apache成爲了市場佔比最高的WEB服務器javascript

數據來自 https://news.netcraft.com
web1.PNG
web2.PNG



這裏簡單記錄下CentOS7.8環境下安裝nginx的步驟與經常使用配置:php

1、安裝

官網 http://nginx.org/en/download....css

  1. 安裝依賴包html

    [root@bluse soft]# yum install -y pcre pcre-devel openssl  openssl-devel gcc gcc-c++ ncurses-devel perl
  2. 下載nginx源碼包並解壓java

    [root@bluse soft]# wget http://nginx.org/download/nginx-1.10.3.tar.gz
    [root@bluse soft]# tar zxvf nginx-1.10.3.tar.gz
  3. 編譯安裝node

    [root@bluse soft]# groupadd nginx
    [root@bluse soft]# useradd nginx -g nginx -s /sbin/nologin -M
    [root@bluse soft]# cd nginx-1.10.3
    [root@bluse nginx-1.10.3]#./configure --prefix=/data/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module
    [root@bluse nginx]#make && make install
  4. 安裝完成後能夠確認下安裝目錄和版本linux

    [root@bluse nginx]# ls
    conf  html  logs  sbin
    [root@bluse nginx]# sbin/nginx -v
    nginx version: nginx/1.10.3
  5. 啓動腳本
    vim /usr/lib/systemd/system/nginx.servicenginx

    [Unit]
    Description=nginx 
    After=network.target 
    
    [Service] 
    Type=forking 
    ExecStart=/data/nginx/sbin/nginx
    ExecReload=/data/nginx/sbin/nginx reload
    ExecStop=/data/nginx/sbin/nginx quit
    PrivateTmp=true 
    
    [Install] 
    WantedBy=multi-user.target

    systemctl daemon-reload
    systemctl enable nginxc++

2、經常使用配置
  • nginx.conf
#運行用戶
user nginx;
 
#啓動進程, 一般設置成和cpu的數量相等; 用cat /proc/cpuinfo查看cpu信息, cpu cores一欄顯示內核數
worker_processes  1;
 
#全局錯誤日誌及PID文件(/data/logs/nginx/logs/error.log)
error_log  /data/logs/nginx/error.log;
#進程id存儲文件(/var/run/nginx.pid)
pid           /var/run/nginx.pid;
#工做模式及鏈接數上限
events {
    use   epoll;                            #epoll是多路複用IO(I/O Multiplexing)中的一種方式,可是僅用於linux2.6以上內核,能夠大大提升nginx的性能
    worker_connections  1024;     #單個後臺worker process進程的最大併發連接數
    # multi_accept on;                #在nginx得到有新鏈接的通知以後,接受盡量多的鏈接, 若是worker_connections設置過低的話,這樣可能會形成擁堵
}
#設定http服務器,利用它的反向代理功能提供負載均衡支持
http {
    #設定mime類型,類型由mime.type文件定義,能夠打開mime.type來看(/usr/local/nginx/conf/mime.types)
    #MIME(Multipurpose Internet Mail Extensions)多用途互聯網郵件擴展類型.
    include       /data/nginx/mime.types;
    #默認文件類型以二進制數據傳輸
    default_type  application/octet-stream;
    #設定日誌文件(/data/logs/nginx/access.log)
    access_log    /data/logs/nginx/access.log;
   
    #關閉在錯誤頁面中的nginx版本數字,這樣對於安全性是有好處的。
    server_tokens off;
 
    #sendfile 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,對於普通應用,
    #必須設爲 on,若是用來進行下載等應用磁盤IO重負載應用,可設置爲 off,以平衡磁盤與網絡I/O處理速度,下降系統的uptime.
    sendfile        on;
   
    #告訴nginx在一個數據包裏發送全部頭文件,而不一個接一個的發送
    #tcp_nopush     on;
 
    #給客戶端分配keep-alive鏈接超時時間
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    #告訴nginx不要緩存數據,而是一段一段的發送;當須要及時發送數據時,就應該給應用設置這個屬性,這樣發送一小塊數據信息時就不能當即獲得返回值
    tcp_nodelay        on;
    
    #開啓gzip壓縮
    gzip  on;
    #指定的客戶端禁用gzip功能。咱們設置成IE6或者更低版本以使咱們的方案可以普遍兼容。
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
 
    #設定請求緩衝
 、、   client_header_buffer_size    1k;
   、、 large_client_header_buffers  4 4k;
 
    open_file_cache max=100000 inactive=20s;     #打開緩存的同時也指定了緩存最大數目,以及緩存的時間; 咱們能夠設置一個相對高的最大時間,這樣咱們能夠在它們不活動超過20秒後清除掉。
    open_file_cace_valid 30s;                                #在open_file_cache中指定檢測正確信息的間隔時間
    open_file_cache_min_uses 2;                           #定義了open_file_cache中指令參數不活動時間期間裏最小的文件數
    open_file_cache_errors on;                              #指定了當搜索一個文件時是否緩存錯誤信息,也包括再次給配置中添加文件。咱們也包括了服務器模塊,這些是在不一樣文件中定義的。若是你的服務器模塊不在這些位置,你就得修改這一行來指定正確的位置。
 
 
    #虛擬主機的配置文件
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
   
    #設定負載均衡的服務器列表
    upstream mysvr {
         #weigth參數表示權值,權值越高被分配到的概率越大
         #本機上的Squid開啓3128端口
         server 192.168.8.1:3128 weight=5;
         server 192.168.8.2:80  weight=1;
         server 192.168.8.3:80  weight=6;
    }
 
    server {
        #偵聽80端口
        listen       80;
        #定義使用www.xx.com訪問
        server_name  www.xx.com;
        #設定本虛擬主機的訪問日誌
        access_log  logs/www.xx.com.access.log  main;
       
        #默認請求
        location / {
            root   /root;      #定義服務器的默認網站根目錄位置
            index index.php index.html index.htm;   #定義首頁索引文件的名稱
            fastcgi_pass  www.xx.com;
            fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name; 
            include /data/nginx/fastcgi_params;
        }
       
        # 定義錯誤提示頁面
        error_page   500 502 503 504 /50x.html;  
             location = /50x.html {
             root   /root;
        }
 
        #靜態文件,nginx本身處理
        location ~ ^/(images|javascript|js|css|flash|media|static)/ {
             root /var/www/virtual/htdocs;
             #過時30天,靜態文件不怎麼更新,過時能夠設大一點,若是頻繁更新,則能夠設置得小一點。
             expires 30d;
        }
 
        #PHP 腳本請求所有轉發到 FastCGI處理. 使用FastCGI默認配置.
        location ~ \.php$ {
             root /root;
             fastcgi_pass 127.0.0.1:9000;
             fastcgi_index index.php;
             fastcgi_param SCRIPT_FILENAME /data/www/www$fastcgi_script_name;
             include fastcgi_params;
        }
 
        #設定查看Nginx狀態的地址
        location /NginxStatus {
             stub_status            on;
             access_log              on;
             auth_basic              "NginxStatus";
             auth_basic_user_file  conf/htpasswd;
        }
 
        #禁止訪問 .htxxx 文件
        location ~ /\.ht {
             deny all;
        }
     
     }
}
" 手動配置基於域名的虛擬主機 "
步驟: 
1. 在nginx.conf中的http段中, 一個server段就表示一個服務主機, 每添加一個虛擬主機, 增長一個server段便可; 
2. 若是單獨新建目錄存放虛擬主機配置文件, 只須要在http段中最後include進來, 如: include vhost/dog.farwish.conf; 
3. 這個dog.farwish.conf中只有server段的配置內容, 如:
 
 
server {
    listen       80;
    server_name  dog.farwish.com;
 
    location / {
        index  index.html index.htm index.php;
        root   /home/www/dog;
        if (!-e $request_filename){   
            rewrite ^(.*)$ /index.php/?s=$1 last; # TP rewrite模式
            break;
        }
 
        autoindex on;
    }   
 
    location ~ \.php {
        root           /data/www/dog;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }   
}
# 以上配置是在nginx1.6.2源碼包配置文件的基礎上改動.
  • 跳轉httpsweb

    server {
     listen 80;
     server_name xx.xx.com;
     rewrite ^(.*) https://$server_name$1 permanent;
     }
    
    server {
     listen 443 ssl;
     server_name xx.xx.com;
     index  index.php index.html index.htm;
     ssl_certificate   cert/server.crt;
     ssl_certificate_key  cert/server.key;
     ssl_session_timeout 5m;
     ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
     ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
     ssl_prefer_server_ciphers on;
     client_max_body_size 100m;
     location / {
       proxy_pass http://172.16.60.170:9000;
     }
    }
相關文章
相關標籤/搜索