1. 程式人生 > 其它 >Nginx從入門到實踐,使用詳解

Nginx從入門到實踐,使用詳解

技術標籤:Nginx

Nginx從入門到實踐,使用詳解

1.Nginx介紹

Nginx 是開源、高效能、高可靠的 Web 和反向代理伺服器,而且支援熱部署,幾乎可以做到 7 * 24
小時不間斷執行,即使執行幾個月也不需要重新啟動,還能在不間斷服務的情況下對軟體版本進行熱更新。

2.使用場景

*靜態資源服務,通過本地檔案系統提供服務
*反向代理服務,延伸出包括快取、負載均衡等
*API 服務,OpenResty 

3.Linux環境下安裝Nginx

前言:基於Centos 7下安裝配置Nginx操作實踐記錄整理

配置EPEL源

sudo yum install -y epel-release
sudo yum -y update

安裝Nginx

sudo yum install -y nginx

結語

安裝成功後,預設的網站目錄為: /usr/share/nginx/html(靜態資源一般放到該目錄下)

預設的配置檔案為:/etc/nginx/nginx.conf

自定義配置檔案目錄為: /etc/nginx/conf.d/

常用命令

檢視nginx執行狀態:systemctl status nginx
開機自啟動nginx:systemctl enable nginx
關閉開機自啟動nginx:systemctl disable nginx
啟動nginx:systemctl start nginx
停止nginx:systemctl stop nginx
重啟nginx:systemctl restart nginx
重新載入配置檔案:nginx -s reload

防火牆

**如果你的伺服器打開了防火牆,你需要開啟80埠。如果你的伺服器是阿里雲的,則需要把埠號加入到安全組**
開啟防火牆:systemctl start firewalld
關閉防火牆:systemctl stop firewalld
重啟防火牆:systemctl restart firewalld
防火牆開啟狀態:systemctl status firewalld
檢視單個埠是否開放:firewall-cmd --query-port=埠號/tcp
新增開放埠(permanent表示永久開啟,不加是臨時開啟重啟之後失效)
firewall-cmd --add-port=埠號/tcp --permanent  或
firewall-cmd --permanent --zone=public --add-port=埠號/tcp
移除開放埠:firewall-cmd --remove-port=埠號/tcp --permanent
檢視開放得埠列表:firewall-cmd --list-ports

nginx.conf典型配置

    user  nginx;                        # 執行使用者,預設即是nginx,可以不進行設定
    worker_processes  1;                # Nginx 程序數,一般設定為和 CPU 核數一樣
    error_log  /var/log/nginx/error.log warn;   # Nginx 的錯誤日誌存放目錄
    pid        /var/run/nginx.pid;      # Nginx 服務啟動時的 pid 存放位置

    events {
    use epoll;     # 使用epoll的I/O模型(如果你不知道Nginx該使用哪種輪詢方法,會自動選擇一個最適合你作業系統的)
    worker_connections 1024;   # 每個程序允許最大併發數
    }

    http {   # 配置使用最頻繁的部分,代理、快取、日誌定義等絕大多數功能和第三方模組的配置都在這裡設定
    # 設定日誌模式
    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;   # 減少網路報文段的數量
    tcp_nodelay         on;
    keepalive_timeout   65;   # 保持連線的時間,也叫超時時間,單位秒
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;      # 副檔名與型別對映表
    default_type        application/octet-stream;   # 預設檔案型別

    include /etc/nginx/conf.d/*.conf;   # 載入子配置項
    
    server {
    	listen       80;       # 配置監聽的埠
    	server_name  localhost;    # 配置的域名
    	
    	location / {
    		root   /usr/share/nginx/html;  # 網站根目錄
    		index  index.html index.htm;   # 預設首頁檔案
    		deny 172.168.22.11;   # 禁止訪問的ip地址,可以為all
    		allow 172.168.33.44;# 允許訪問的ip地址,可以為all
    	}
    	
    	error_page 500 502 503 504 /50x.html;  # 預設50x對應的訪問頁面
    	error_page 400 404 error.html;   # 同上
    }
    }

語法規則

配置檔案由指令與指令塊構成;
每條指令以 ; 分號結尾,指令與引數間以空格符號分隔;
指令塊以 {} 大括號將多條指令組織在一起;
include 語句允許組合多個配置檔案以提升可維護性;
使用 # 符號添加註釋,提高可讀性;
使用 $ 符號使用變數;
部分指令的引數支援正則表示式;

4.反向代理配置

5.靜態資源配置

4.參考文獻

	(https://developer.aliyun.com/article/699966)
	(https://mp.weixin.qq.com/s/hafErlto-1N6ypYfOVIGBQ)