初识nginx

Posted 小张敲代码

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了初识nginx相关的知识,希望对你有一定的参考价值。

一、centos系统下载安装

nginx下载:http://nginx.org/download/nginx-1.18.0.tar.gz
openresty下载:https://openresty.org/download/openresty-1.17.8.2.tar.gz

//安装nginx步骤wget http://nginx.org/download/nginx-1.18.0.tar.gztar -xzvf nginx-1.18.0.tar.gzcd nginx-1.18.0.tar.gz//安装到/usr/local/nginx目录下./configure --prefix=/usr/local/nginxmakemake install
openresty的安装过程和nginx一样,可以不加--prefix,默认安装路径/usr/local/openresty

二、基本命令操作

[root@iZwz9he4lwjhlld6pjd1yaZ /]# cd /usr/local/nginx/sbin/[root@iZwz9he4lwjhlld6pjd1yaZ sbin]# ls -altotal 3772drwxr-xr-x 2 root root 4096 Mar 29 23:17 .drwxr-xr-x 12 root root 4096 Mar 30 00:16 ..-rwxr-xr-x 1 root root 3851568 Mar 29 23:17 ngin
  • ./nginx                    启动nginx

  • ./nginx -s reload    重载

  • ./nginx -s stop       停止服务

  • ./nginx -s quit        优雅的停止服务

  • ./nginx -v               打印版本信息

  • ./nginx -V               打印编译信息


三、日志切割

启动一个定时任务去执行以下的shell脚本,实现日志切割

[root@iZwz9he4lwjhlld6pjd1yaZ logs]# crontab -l0 3 * * * /usr/local/nginx/logs/rotate.sh


#!/bin/bashLOGS_PATH=/usr/local/nginx/logs/historyCUR_LOGS_PATH=/usr/local/nginx/logsYESTERDAY=$(date -d "yesterday" +%Y-%m-%d)mv ${CUR_LOGS_PATH}/access.log ${LOGS_PATH}/access_${YESTERDAY}.logmv ${CUR_LOGS_PATH}/error.log ${LOGS_PATH}/error_${YESTERDAY}.log
/usr/local/nginx/sbin/nginx -s reopen

四、搭建一个静态资源服务器

//新建一个dlib目录,在dlib目录下放置一些静态文件[root@iZwz9he4lwjhlld6pjd1yaZ nginx]# ls -altotal 48drwxr-xr-x 12 root root 4096 Mar 30 00:16 .drwxr-xr-x. 15 root root 4096 Mar 30 23:07 ..drwx------ 2 nobody root 4096 Mar 30 00:15 client_body_tempdrwxr-xr-x 2 root root 4096 Mar 31 00:19 confdrwxr-xr-x 2 root root 4096 Mar 30 00:16 dlibdrwx------ 2 nobody root 4096 Mar 30 00:15 fastcgi_tempdrwxr-xr-x 2 root root 4096 Mar 29 23:17 htmldrwxr-xr-x 3 root root 4096 Mar 30 23:32 logsdrwx------ 2 nobody root 4096 Mar 30 00:15 proxy_tempdrwxr-xr-x 2 root root 4096 Mar 29 23:17 sbindrwx------ 2 nobody root 4096 Mar 30 00:15 scgi_tempdrwx------ 2 nobody root 4096 Mar 30 00:15 uwsgi_temp[root@iZwz9he4lwjhlld6pjd1yaZ nginx]# tree dlib/dlib/├── a.txt├── b.txt└── c.txt
0 directories, 3 files[root@iZwz9he4lwjhlld6pjd1yaZ nginx]#
//以下为nginx.conf配置http { include mime.types; default_type application/octet-stream;
#定义日志格式,格式名称为main log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
#access访问日志使用了main格式的日志 access_log logs/access.log main;
sendfile on; #tcp_nopush on;
#keepalive_timeout 0; keepalive_timeout 65;
#on or off表示是否开启gzip gzip on; #1表示小于1字节就不压缩了,可以设置大一点,这边测试使用 gzip_min_length 1; #压缩级别 gzip_comp_level 2; #压缩文件类型 gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
server { listen 8080; server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# 斜杠表示所有请求 location / { #所有请求和dlib目录一一对应 alias dlib/; #打开目录浏览功能 autoindex on; #限制访问速率,这边是每秒1kb set $limit_rate 1k; #root html; #index index.html index.htm; }
#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; } }}

五、搭建一个具备缓存功能的反向代理服务器

这边我是安装了nginx和openresty,使用openresty做反向代理


  • nginx配置

 server { #在端口号前加上127.0.0.1,就只有本机能访问了,浏览器就访问不到了 listen 127.0.0.1:8080; server_name localhost;}
  • openresty配置


http { include mime.types; default_type application/octet-stream;
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;
sendfile on; #tcp_nopush on;
#keepalive_timeout 0; keepalive_timeout 65;
#gzip on; #上游服务,名称叫local upstream local { server 127.0.0.1:8080; #这边可以继续加服务器配置做负载均衡 }
#缓存配置,名称为my_cache proxy_cache_path /tmp/nginxcache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
server { listen 80; server_name 你的域名; #charset koi8-r; #access_log logs/host.access.log main;
location / { #拿到远端真实的host ip等 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#使用上面缓存配置的名称my_cache proxy_cache my_cache; proxy_cache_key $host$uri$is_args$args; proxy_cache_valid 200 304 302 1d;
#root html; #index index.html index.htm; #反向代理 proxy_pass http://local; } }}

以上是关于初识nginx的主要内容,如果未能解决你的问题,请参考以下文章

Java初识方法

Nginx初识

初识Nginx

初识OpenGL 片段着色器(Fragment Shader)

初识OpenGL 片段着色器(Fragment Shader)

初识nginx——内存池篇