系列目錄
Nginx簡介
Nginx是一個免費的,開源的,高性能的HTTP服務器和反向代理,以及IMAP / POP3代理服務器。
Nginx以其高性能,穩定性,豐富的功能集,簡單的配置和低資源消耗而聞名。
Nginx使用更加可擴展的事件驅動(異步)架構,此體系結構在負載下使用較小但更重要的可預測內存量。即使您不希望同時處理數千個請求,您仍然可以從Nginx的高性能和小內存占用中受益。
Nginx可以向各個方向擴展:從最小的VPS一直到大型服務器集群。
安裝Nginx
安裝 epel
sudo yum install epel-release
安裝 Nginx
sudo yum install nginx
啟動 Nginx
Nginx 不會自己啟動,啟動命令:
sudo systemctl start nginx
關閉防火牆
如果外部瀏覽器輸入該機IP還是訪問不了,說明有防火牆正在運行,關閉HTTP 和 HTTPS的防火牆:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
訪問驗證
外部瀏覽器訪問成功,說明Nginx安裝成功。
開機啟動
最后,因為Nginx默認是不主動開啟的,為了能夠在系統啟動就開啟Nginx:
sudo systemctl enable nginx
端口映射配置
查看nginx.conf
vi /etc/nginx/nginx.conf
修改nginx配置
nginx.conf文件http配置內容,並且i
進去注釋掉http配置下server的默認配置內容Esc
+:wq
保存后退出。
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;
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;
#【解釋】nginx會加載 /etc/nginx/conf.d文件夾下所有的conf文件的配置
# server {
# listen 80 default_server;
# listen [::]:80 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# # 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 {
# }
# }
創建新配置
根據nginx配置文件有提示,在/etc/nginx/conf.d文件夾下為hellocore項目新建一個netcore.conf文件,文件配置如下
server {
listen 80;
location / {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
重啟nginx
nginx -s reload # systemctl restart nginx
特別注意
外部訪問有可能會報502錯誤
原因:
SELinux配置問題
解決:
方法1.關閉SELinux
輸入:sestatus
,如果SELinux status: enabled
,表示開啟,輸入vi /etc/selinux/config
修改配置:SELINUX=disabled
。
方法2.將nginx添加至SELinux的白名單
逐行執行如下命令:
yum install policycoreutils-python
cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx
semodule -i mynginx.pp