Nginx初步學習

注意事項

  1. 環境: centos7, nginx1.1.6
  2. 在Windows和win10上的linux中作測試,發現配置總也不能生效,並且還少一些必要的文件,因此不建議在Windows和win10上的linux中學習.

安裝

  1. yum安裝必要的程序
yum -y install gcc gcc-c++ autoconf pcre-devel make automake
yum -y install wget httpd-tools vim
複製代碼
  1. 配置yum源

vim /etc/yum.repos.d/nginx.repophp

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
複製代碼
  1. 安裝nginx
yum install nginx
nginx -v
複製代碼

nginx配置文件

/etc/nginx/nginx.confhtml

#運行用戶,默認便是nginx,能夠不進行設置
user  nginx;
#Nginx進程,通常設置爲和CPU核數同樣
worker_processes  1;   
#錯誤日誌存放目錄
error_log  /var/log/nginx/error.log warn;
#進程pid存放位置
pid        /var/run/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;   #nginx訪問日誌存放位置

    sendfile        on;   #開啓高效傳輸模式
    #tcp_nopush on; #減小網絡報文段的數量

    keepalive_timeout  65;  #保持鏈接的時間,也叫超時時間

    #gzip on; #開啓gzip壓縮

    include /etc/nginx/conf.d/*.conf; #包含的子配置項位置和文件
複製代碼

/etc/conf.d/default.conflinux

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; # 配置404頁面

    # 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;
    #}
}
複製代碼

nginx相關命令

  1. nginx啓動
方式1: 
nginx

方式2: 
systemctl start nginx.service
複製代碼
  1. nginx中止
(1) 從容中止服務
nginx -s quit

(2) 當即中止服務
nginx  -s stop

(3) 殺死進程
killall nginx

(4) 服務命令中止
systemctl stop nginx.service
複製代碼
  1. nginx重啓
(1) 從新載入配置文件
nginx -s reload
(2) 服務方式中止
systemctl restart nginx.service
複製代碼

nginx設置虛擬主機

  1. 基於端口號設置虛擬主機
  • nginx默認監聽80端口,此處經過監聽8001端口,服務啓動目錄指向/usr/share/nginx/html/html8001,當用戶訪問http://localhost:8001時,會訪問/usr/share/nginx/html/html8001下的index.html.
  • 經過編寫多個server,達到多個端口號設置虛擬主機的效果
server{
        listen 8001;
        server_name localhost;
        root /usr/share/nginx/html/html8001;
        index index.html;
}
複製代碼
  1. 基於ip/域名設置虛擬主機
  • 相似於用端口設置虛擬主機,此處使用server_name來設置不一樣的虛擬主機
  • 經過編寫多個server,達到多個ip/域名設置虛擬主機的效果
server{
        listen 80;
        server_name 112.74.164.244;
        root /usr/share/nginx/html/html8001;
        index index.html;
}
複製代碼

nginx反向代理

下面代碼意思是監聽80端口,當用戶訪問nginx2.jspang.com時,將用戶訪問代理到http://jspang.com域名下.nginx

server{
        listen 80;
        server_name nginx2.jspang.com;
        location / {
               proxy_pass http://jspang.com;
        }
}
複製代碼

其餘反向代理命令c++

  • proxy_set_header :在將客戶端請求發送給後端服務器以前,更改來自客戶端的請求頭信息。web

  • proxy_connect_timeout:配置Nginx與後端代理服務器嘗試創建鏈接的超時時間。vim

  • proxy_read_timeout : 配置Nginx向後端服務器組發出read請求後,等待相應的超時時間。後端

  • proxy_send_timeout:配置Nginx向後端服務器組發出write請求後,等待相應的超時時間。centos

  • proxy_redirect :用於修改後端服務器返回的響應頭中的Location和Refresh。bash

$http_user_agent使用

  • if (http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') 的意思是若是在http_user_agent存在(Android|webOS|iPhone|iPod|BlackBerry),則認爲是手機訪問,將項目啓動根目錄設置到手機部署目錄
  • 在開發中,可能會將pc和phone分開爲不一樣的域名,如pc: www.xxx.com; phone: m.xxx.com; 此時應該經過proxy_pass來實現需求.
server{
        listen 80;
        server_name nginx2.jspang.com;
        location / {
         root /usr/share/nginx/pc;
         if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
            root /usr/share/nginx/mobile;
         }
         index index.html;
        }
}
複製代碼
相關文章
相關標籤/搜索