Linux——yum安装nginx 1.17+

参考:https://nginx.org/en/linux_packages.html
1、准备yum

sudo yum -y install yum-utils

2、配置nginx repos

vi /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

3、完成nginx-mainline配置

sudo yum-config-manager --enable nginx-mainline

4、安装nginx

sudo yum install nginx

5、相关目录
主要目录

nginx 日志文件 /var/log/nginx
nginx配置文件目录 /etc/nginx
nginx 可执行文件 /usr/sbin/nginx
nginx环境配置 /etc/sysconfig/nginx
nginx默认站点目录  /usr/share/nginx/html

其他目录

/etc/logrotate.d/nginx
/var/cache/nginx
/usr/lib64/nginx
/usr/libexec/initscripts/legacy-actions/nginx

6、常用命令
#测试配置文件正确性

nginx -t

#信号命令

nginx -s [signal]
nginx -s reload 刷新配置
nginx -s fast 快速stop
nginx -s graceful 优雅stop

#启停服务

service nginx status | stop | start

7、设置开机启动
vim /etc/init.d/nginx


#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
 
nginxd=/usr/sbin/nginx    
nginx_config=/etc/nginx/nginx.conf 
nginx_pid=/var/run/nginx.pid    
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
    if [ -e $nginx_pid ];then
       echo "nginx already running...."
   exit 1
    fi
       echo -n $"Starting $prog: "
       daemon $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
       [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
   return $RETVAL
}
# Stop nginx daemons functions.
stop() {
    echo -n $"Stopping $prog: "
    killproc $nginxd
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
    echo -n $"Reloading $prog: "
    #kill -HUP `cat ${nginx_pid}`
    killproc $nginxd -HUP
    RETVAL=$?
    echo
}
# See how we were called.
    case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
reload)
    reload
    ;;
restart)
    stop
    start
    ;;
status)
    status $prog
    RETVAL=$?
    ;;
*)
    echo $"Usage: $prog {start|stop|restart|reload|status|help}"
    exit 1
esac
exit $RETVAL

赋权

chmod 755 /etc/init.d/nginx  

加入开机启动

chkconfig --add nginx
chkconfig nginx on

跨域设置


   location /xxx{

    if ($request_method = 'OPTIONS') {
      add_header Access-Control-Allow-Origin *;
      add_header Access-Control-Max-Age 1728000;
      add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
      add_header Access-Control-Allow-Headers  'Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With,userId,token,centralToken';
      add_header Content-Type' 'text/plain; charset=utf-8';
      add_header Content-Length 0 ;
      return 200;
     }

     #add_header Access-Control-Allow-Origin *;
     add_header Access-Control-Allow-Origin "$http_origin" always;
     add_header Access-Control-Allow-Credentials true;
     add_header Access-Control-Allow-Methods GET,POSR;
     add_header Access-Control-Allow-Headers 'Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With,userId,token,centralToken';

    proxy_pass http://127.0.0.1:/xxx/;
    proxy_redirect off;
    proxy_set_header Host $host:$server_port;
    proxy_set_header X-Real-IP $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 5;
    proxy_send_timeout 15;
    proxy_read_timeout 15;
    proxy_buffer_size 4k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;
    #proxy_http_version 1.1;
    #proxy_set_header Connection "";
    #proxy_set_header Upgrade $http_upgrade;
    #proxy_set_header Connection "upgrade";
    #fastcgi_param  SCRIPT_NAME        "";

  }

9、配置404、500错误页

    proxy_intercept_errors on; 
    fastcgi_intercept_errors on;
    error_page 400 401 402 403 404 405 408 410 412 413 414 415 /404.html;
    error_page 500 501 502 503 504 506 /500.html;

      location = /404.html {
          root /etc/nginx/html/;
      }

      location = /500.html {
          root /etc/nginx/html/;
      }

10、nginx与后端服务保持keepalive

  upstream  gw_server {
      server 127.0.0.1:8602;
      ip_hash;
      keepalive 200;
  }
   location / {
          proxy_pass http://gw_server/;
          proxy_http_version 1.1;
          proxy_set_header Connection "";
   }

11、日志统计

1).根据访问IP统计UV
awk '{print $1}'  access.log|sort | uniq -c |wc -l

2).统计访问URL统计PV
awk '{print $7}' access.log|wc -l

3).查询访问最频繁的URL
more access.log | awk '{print $7}' |sort | uniq -c |sort -n -k 1 -r|more
4).查询访问最频繁的IP
more  access.log | awk '{print $1}'|sort | uniq -c |sort -n -k 1 -r|more

5).根据时间段统计查看日志
 cat  access.log| sed -n '/14\/Mar\/2018:21/,/14\/Mar\/2018:22/p'|more
统计时间段内,访问最频繁的URL
cat  access.log| sed -n '/28\/May\/2020:14/,/28\/May\/2020:22/p'| awk '{print $7}' |sort | uniq -c |sort -n -k 1 -r|more
统计时间段内,访问最频繁的IP
cat  access.log| sed -n '/28\/May\/2020:14/,/28\/May\/2020:22/p'| awk '{print $1}' |sort | uniq -c |sort -n -k 1 -r|more

增加设置支持vue history模式
先在nginx的主目录下建立目录软链接。

cd /etc/nginx/html
ln -s /data/pcact/ pcact0

nginx中增加配置配置

try_files $uri $uri/ /pcact0/experience/index.html;
 location /pcact/experience/{
     alias /data/pcact/;
     index index.html;
     try_files $uri $uri/ /pcact0/experience/index.html;
}
           open_file_cache max=10000 inactive=20s; 
           open_file_cache_valid 30s;      
           open_file_cache_min_uses 3;
           open_file_cache_errors off;

JSON 日志格式

log_format json '{"@timestamp":"$time_iso8601",'
               '"host":"$server_addr",'
               '"clientip":"$remote_addr",' '"size":$body_bytes_sent,'
               '"responsetime":$request_time,'
               '"upstreamtime":"$upstream_response_time",'
               '"upstreamhost":"$upstream_addr",'
               '"request_method":"$request_method",'
               '"http_host":"$host",'
               '"url":"http://$http_host$request_uri",'
               '"xff":"$http_x_forwarded_for",'
               '"referer":"$http_referer",'
               '"agent":"$http_user_agent",'
               '"status":"$status"}';
access_log  /var/log/nginx/json-access.log  json;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值