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