Nginx
下载
官网下载
安装
从CentOS 8开始,Nginx软件包在默认的CentOS存储库中可用。
使用root用户执行:
- 安装Nginx:
yum install -y nginx
- 启动Nginx:
systemctl start nginx.service
- 设置开机自动运行:
systemctl enable nginx.service
Nginx的默认安装路径
- Nginx配置路径:
/etc/nginx/
- PID目录:
/var/run/nginx.pid
- 错误日志:
/var/log/nginx/error.log
- 访问日志:
/var/log/nginx/access.log
- 默认站点目录:
/usr/share/nginx/html
配置
默认配置文件(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;
# 工作进程数
worker_processes auto;
# 全局错误日志及PID文件
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
# 工作模式及连接数上限
events {
# 单个后台worker process进程的最大并发链接数
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;
# sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime。
sendfile on;
tcp_nopush on;
tcp_nodelay on;
# 连接超时时间
keepalive_timeout 65;
types_hash_max_size 2048;
# 设定mime类型,类型由mime.type文件定义
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 {
# 侦听80端口
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 {
}
}
}
静态资源代理
标准配置如下,请根据实际情况修改 1~6
步骤中的配置,将该配置保存为 test.conf
(名称自定),上传至 /etc/nginx/conf.d
下。
# http访问重定向至https
server{
listen 80;
# 1. 请自行修改test.dongyunit.com(访问域名)
server_name test.dongyunit.com;
rewrite ^(.*)$ https://$host$1 permanent;
}
# https访问
server {
listen 443 ssl http2;
# 2. 请自行修改test.dongyunit.com(访问域名)
server_name test.dongyunit.com;
# 3. 请自行修改cert/test.dongyunit.com.pem(证书pem文件路径)
ssl_certificate cert/test.dongyunit.com.pem;
# 4. 请自行修改cert/test.dongyunit.com.key(证书key文件路径)
ssl_certificate_key cert/test.dongyunit.com.key;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
# 5. 请自行修改访问路径,一般为”/“
location / {
# 6. 请自行修改项目文件路径
root /usr/share/nginx/html/test;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
}
Tomcat虚拟主机代理
标准配置如下,请根据实际情况修改 1~7
步骤中的配置,,将该配置保存为 test.conf
(名称自定),上传至 /etc/nginx/conf.d
下。
# test服务器集群(8005,8080)
# 1. 请自行修改testTomcat
upstream testTomcat{
# 2. 请自行修改124.22.88.232:8080(IP+后台服务端口)
server 124.22.88.232:8080 weight=1;
}
# http访问重定向至https
server{
listen 80;
# 3. 请自行修改test.dongyunit.com(访问域名)
server_name test.dongyunit.com;
rewrite ^(.*)$ https://$host$1 permanent;
}
#https访问
server {
listen 443 ssl;
# 4. 请自行修改test.dongyunit.com(访问域名)
server_name test.dongyunit.com;
# 5. 请自行修改cert/test.dongyunit.com.pem(证书pem文件路径)
ssl_certificate cert/test.dongyunit.com.pem;
# 6. 请自行修改cert/test.dongyunit.com.key(证书key文件路径)
ssl_certificate_key cert/test.dongyunit.com.key;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
# 7. 请自行修改testTomcat,与第 1 条保持一致
proxy_pass http://testTomcat/;
proxy_redirect default;
#后端的Web服务器可以通过X-Forwarded-For>获取用户真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffers 6 32k; #proxy_buffers缓冲区,网页平均在32k以下的话这样设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
}
}
其他
- 启动Nginx:
systemctl start nginx.service
- 停止Nginx:
systemctl stop nginx.service
- 重启Nginx:
systemctl restart nginx.service
- 查看Nginx状态:
systemctl status nginx.service