资源准备
官网: nginx news
安装包下载地址: Index of /download/
Nginx安装
1.1将资源上传到/home/diusr/tools/nginx目录下
1.2 离线安装GCC
进入gcc目录下进行命令执行
# 执行命令(安装离线包) rpm -Uvh *.rpm --nodeps --force
# 查询是否成功 gcc -v
1.3离线安装GCC-C++
进入gcc-c++目录下进行命令执行
# 执行命令(安装离线包) rpm -Uvh *.rpm --nodeps --force
# 查询是否成功 g++ -v
1.4安装pcre
# cd /home/diusr/tools/nginx
# tar -zxvf pcre-8.35.tar.gz
# cd pcre-8.35
# ./configure
# make
# make install
1.5安装 libtool
# cd /home/diusr/tools/nginx
# tar -zxvf libtool-2.4.2.tar.gz
# cd libtool-2.4.2
# ./configure
# make
# make install
1.6安装Nginx
# cd /home/diusr/tools/nginx
# tar -zxvf nginx-1.20.0.tar.gz
# cd nginx-1.20.0
# ./configure
# make
# make install
1.6.1启动Nginx(直接用默认配置启动测试即可)
# cd /usr/local/nginx/sbin
#./nginx
1.6.2开放端口(开放nginx默认使用的80端口,并重启防火墙)
# 开放80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
# 立即生效
# firewall-cmd --reload
1.6.3测试验证
1.6.4设置nginx开机自启
第一步---> 在/etc/systemd/system目录创建一个启动脚本
vim /etc/systemd/system/nginx.service
第二步---> 输入一下内容
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
第三步---> 执行
# 查找nginx端口 ps -ef|grep nginx
# 先停止 nginx kill -9 xxx
# 执行 systemctl daemon-reload
# 开机自启 systemctl enable nginx
1.6.5 常用执行命令
# 启动nginx
systemctl start nginx
#设置开机自启动
systemctl enable nginx
#停止开机自启动
systemctl disable nginx
#查看服务当前状态
systemctl status nginx
#重新启动服务
systemctl restart nginx
#查看所有已启动的服务
systemctl list-units --type=service
1.6.6 配置管理
# 修改nginx配置
vim /usr/local/nginx/conf/nginx.conf
user root;
worker_processes 8;#启动进程,通常设置成和cpu的数量相等或者2倍于cpu的个数(具体结合cpu和内存)。默认为1
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;#全局的错误日志和日志级别[ debug | info | notice | warn | error | crit ]
#pid logs/nginx.pid;
worker_rlimit_nofile 100000;
events {
worker_connections 10240;
use epoll;#是多路复用IO(I/O Multiplexing)中的一种方式,仅用于linux2.6以上内核,可以大大的提高nginx的性能
}
http {
include mime.types;#设定mime类型,类型由mime.type文件定义。文件扩展名与文件类型映射表
default_type application/octet-stream;#默认文件类型
server_tokens off;#关闭在错误页面中的nginx版本数字
server_names_hash_bucket_size 128;
#设定日志格式
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 logs/access.log main;#access日志文件的路径,采用上面定义的main 格式记录
#access_log off;
sendfile on;#以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。默认开启状态
tcp_nopush on;#nginx在一个数据包里发送所有头文件,而不一个接一个的发送 ,防止网络阻塞
tcp_nodelay on;
#keepalive_timeout 0;
keepalive_timeout 60;#给客户端分配keep-alive链接超时时间。服务器将在这个超时时间过后关闭链接
#FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 128k;
fastcgi_buffers 2 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
client_max_body_size 50M; #设置允许发布内容为50M
#gzip模块设置
gzip on;#gzip模块设置
gzip_proxied any;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 2;
gzip_types text/plain text/css application/x-javascript application/javascript application/xml application/json image/jpeg image/gif image/png;
gzip_disable "MSIE [1-6]\.";
#开启限制IP连接数的时候需要使用
#limit_zone crawler $binary_remote_addr 10m;
#平台服务端负载均衡配置
upstream dmjservice {
server xx.xx.xx.xxx:xxxx;
}
#平台对外接口服务端负载均衡配置
upstream dmjapi {
server xxx.xxx.xxx.xx:xxx;
}
#平台对外接口服务端负载均衡配置
upstream dmjfdfs {
server xxx.xxx.xxx.xxx:xxxx;
}
server {
listen 8088;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
root /home/diusr/xx/xx-web/dist;
location ^~ /static/ {
root /home/diusr/xx/xx-web/dist;
index index.html index.htm;
}
location ^~/dmj-service/ {
proxy_pass http://dmjservice;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_connect_timeout 10;
proxy_read_timeout 18000;
proxy_next_upstream http_502 http_504 error invalid_header;
}
location ^~/dmj-api/ {
proxy_pass http://dmjapi;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_connect_timeout 10;
proxy_read_timeout 18000;
proxy_next_upstream http_502 http_504 error invalid_header;
}
location ^~/dmj-fdfs/ {
proxy_pass http://dmjfdfs;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_connect_timeout 10;
proxy_read_timeout 18000;
proxy_next_upstream http_502 http_504 error invalid_header;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
}
精彩链接
发表评论