一、安裝Nginx
Nginx是一款輕量級高性能的Web服務器和反向代理服務器,被廣泛使用於互聯網領域,特別是在高並發的情況下。在CentOS中安裝Nginx非常簡單,只需要執行以下命令:
yum install nginx
執行完上述安裝命令後,Nginx就已經安裝並運行了。可以使用以下命令啟動、停止、重啟、重新加載配置:
systemctl start nginx #啟動Nginx systemctl stop nginx #停止Nginx systemctl restart nginx #重啟Nginx systemctl reload nginx #重新加載Nginx配置(不會中斷正在進行的連接)
二、設置Nginx的基本配置
默認情況下,Nginx的配置文件路徑為/etc/nginx/nginx.conf。在該配置文件中可以配置Nginx的基本設置,比如監聽的端口、訪問日誌等。打開配置文件後,可以進行一些基本設置:
worker_processes 1; #指定工作進程數 error_log /var/log/nginx/error.log warn; #錯誤日誌 pid /var/run/nginx.pid; #PID文件保存路徑 events { worker_connections 1024; } #設置work連接數 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; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; } #http的全局配置
三、配置虛擬主機
虛擬主機,常見於Web服務器,允許在同一台服務器上運行多個網站,每個網站有自己的域名或IP地址。虛擬主機的配置,需要在Nginx的配置文件/etc/nginx/nginx.conf中進行,可以在文件底部添加下面的代碼:
server { listen 80; server_name example.com; location / { root /usr/share/nginx/html; index index.html index.htm; } }
上述代碼會為example.com域名創建一個虛擬主機,並且指定該虛擬主機使用的端口是80。其中,location指令定義了Web服務器如何響應URI,root指令指定了Web服務器文件的根目錄。
四、設置HTTPS
使用HTTPS可以保證網站和用戶之間的數據傳輸是安全的。Nginx支持HTTPS,可以使用以下命令進行安裝:
yum install -y nginx openssl
安裝完以上兩個軟件包後,就可以進行HTTPS配置了。首先需要生成SSL證書文件,在命令行中執行:
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/cert.key -out /etc/nginx/cert.crt
執行完上述命令後,會在/etc/nginx目錄下生成一個cert.key和cert.crt文件,接下來在Nginx的配置文件/etc/nginx/nginx.conf中添加以下內容:
server { listen 443 ssl; server_name example.com; ssl_certificate /etc/nginx/cert.crt; ssl_certificate_key /etc/nginx/cert.key; location / { root /usr/share/nginx/html; index index.html index.htm; } }
注意,需要將server監聽端口改為443,並添加ssl指令來開啟HTTPS服務。其中,ssl_certificate指令指定證書文件位置,ssl_certificate_key指令指定私鑰文件位置。
五、啟用Nginx緩存
啟用Nginx緩存可以提高網站性能,大大減少後端服務器的壓力,同時可以提供更快的訪問速度。Nginx緩存可以設置為三種模式:fastcgi、proxy和memcached。下面以proxy模式為例:
upstream myserver { server backend1.example.com; server backend2.example.com; } server { listen 80; server_name example.com; location / { proxy_pass http://myserver; proxy_cache_bypass $http_pragma; proxy_cache_revalidate on; proxy_cache_valid 200 302 10m; proxy_cache_valid 404 1m; proxy_cache_key "$host$request_uri"; proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m; proxy_temp_path /var/cache/nginx/temp; add_header X-Proxy-Cache $upstream_cache_status; } }
上述代碼使用了proxy模式,創建了一個名稱為myserver的upstream,指定了多個後端服務器。然後在location指令中引用myserver,其後設置緩存的相關指令。
六、啟用Nginx負載均衡
Nginx可以使用負載均衡來提高網站並發量,同時也可以將負載均衡作為一種擴展手段來進行服務器架構的設計。Nginx提供了多種負載均衡算法,如輪詢、IP哈希、加權輪詢等,在使用時需要根據實際情況進行選擇。以下是一個簡單的Nginx負載均衡配置示例:
upstream myserver { server backend1.example.com weight=5; server backend2.example.com; server backend3.example.com; } server { listen 80; server_name example.com; location / { proxy_pass http://myserver; } }
上述代碼定義了一個名為myserver的upstream,其中backend1的權重為5,backend2和backend3的權重均為1。在location指令中引用myserver即可實現負載均衡。
七、總結
本文主要介紹了在CentOS中安裝Nginx,以及如何進行基本配置、設置虛擬主機、啟用HTTPS、啟用Nginx緩存、啟用Nginx負載均衡等。通過本文的介紹,您應該已經對Nginx有了更深入的了解,能夠更好地進行Web服務器的架設和性能優化。
原創文章,作者:HJLOH,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/317726.html