如何在CentOS 7上安裝和配置Nginx
阿新 • • 發佈:2019-02-11
1.安裝CentOS 7 EPEL倉庫
sudo yum install epel-release
2.安裝Nginx
現在Nginx儲存庫已經安裝在您的伺服器上,使用以下yum
命令安裝Nginx :
sudo yum install nginx
在對提示回答yes後,Nginx將在伺服器上完成安裝。
3.啟動Nginx
Nginx不會自行啟動。要執行Nginx,請輸入:
sudo systemctl start nginx
如果您正在執行防火牆,請執行以下命令以允許HTTP和HTTPS通訊:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
開啟瀏覽器輸入ip地址看到nginx的首頁就說明你啟動成功了
4.設定開機啟動
sudo systemctl enable nginx
5.配置nginx
使用yum
進行安裝的nginx的配置檔案在/etc/nginx/nginx.conf
vim /etc/nginx/nginx.conf
nginx.conf:
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
#nginx程序數,建議設定為等於CPU總核心數。
worker_processes auto;
#全域性錯誤日誌定義型別,[ debug | info | notice | warn | error | crit ]
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;#程序pid檔案
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
#單個程序最大連線數(最大連線數=連線數*程序數)
#根據硬體調整,和前面工作程序配合起來用,儘量大,但是別把cpu跑到100%就行。每個程序允許的最多連線數,理論上每臺nginx伺服器的最大連線數為。
worker_connections 1024;
}
#設定http伺服器,利用它的反向代理功能提供負載均衡支援
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;
#開啟高效檔案傳輸模式,sendfile指令指定nginx是否呼叫sendfile函式來輸出檔案,對於普通應用設為 on,如果用來進行下載等應用磁碟IO重負載應用,可設定為off,以平衡磁碟與網路I/O處理速度,降低系統的負載。注意:如果圖片顯示不正常把這個改成off。
#sendfile指令指定 nginx 是否呼叫sendfile 函式(zero copy 方式)來輸出檔案,對於普通應用,必須設為on。如果用來進行下載等應用磁碟IO重負載應用,可設定為off,以平衡磁碟與網路IO處理速度,降低系統uptime。
sendfile on;
#此選項允許或禁止使用socke的TCP_CORK的選項,此選項僅在使用sendfile的時候使用
tcp_nopush on;
tcp_nodelay on;
#長連線超時時間,單位是秒
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
#虛擬主機的配置
server {
#監聽埠
listen 80 default_server;
listen [::]:80 default_server;
#域名可以有多個,用空格隔開
server_name luischen.cn;
# root /usr/share/nginx/html;
# 重定向至https(按照需求)
rewrite ^(.*)$ https://$host$1 permanent;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
server {
# 監聽433埠
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name luischen.cn;
#root /usr/share/nginx/html;
# ssl證書
ssl_certificate "/etc/nginx/1_luischen.cn_bundle.crt";
ssl_certificate_key "/etc/nginx/2_luischen.cn.key";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
#以下是一些反向代理的配置,可選。
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
#後端的Web伺服器可以通過X-Forwarded-For獲取使用者真實IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
location / {
# 需要代理的埠-也就是nginx指向本地的埠
proxy_pass http://127.0.0.1:8091;
# 超時時間
proxy_connect_timeout 600;
proxy_read_timeout 600;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
6.nginx啟動和停止命令
啟動
sudo systemctl start nginx
停止
sudo systemctl stop nginx