首页 > 系统相关 >Nginx

Nginx

时间:2023-08-27 21:56:05浏览次数:28  
标签:nginx data Nginx html conf root localhost

目录

Nginx

编译安装nginx

[root@localhost /]#cd data
#进入到data目录,把nginx包下载到该目录下

[root@localhost data]#yum -y install gcc pcre-devel openssl-devel zlib-devel openssl  openssl-devel
#下载nginx包所需要的环境依赖包

[root@localhost data]#useradd -M -s /sbin/nologin nginx
#创建一个nginx用户用于管理

[root@localhost data]#mkdir /apps/nginx -p
#创建一个nginx存放路径包

[root@localhost data]#wget http://nginx.org/download/nginx-1.18.0.tar.gz
#官网下载nginx安装包

[root@localhost data]#ls
nginx-1.18.0.tar.gz
#查看nginx1.18.0安装包

[root@localhost data]#tar xf nginx-1.18.0.tar.gz 
#解压nginx1.18.0包

[root@localhost data]#ls
nginx-1.18.0  nginx-1.18.0.tar.gz
#查看解压后的包

[root@localhost data]#cd nginx-1.18.0/
[root@localhost nginx-1.18.0]#ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
#CHANGES     告诉nginx1.18.0修复了什么bug
#CHANGES.ru   俄文版告诉nginx1.18.0修复了什么bug
#conf         默认的配置文件
#configure    安装脚本
#contrib       提供一个vim编辑器的效果
#html          网页文件
#man           man帮助
#src           源码


[root@localhost nginx-1.18.0]#./configure --prefix=/apps/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_ssl_module \
> --with-http_v2_module \
> --with-http_realip_module \
> --with-http_stub_status_module \
> --with-http_gzip_static_module \
> --with-pcre \
> --with-stream \
> --with-stream_ssl_module \
> --with-stream_realip_module


[root@localhost nginx-1.18.0]#make -j2 && make install
#编译


[root@localhost nginx]#ln -s /apps/nginx/sbin/nginx  /usr/sbin
#做一个软连接

[root@localhost sbin]#/apps/nginx/sbin/nginx
#绝对路径启动nginx


[root@localhost ~]#chown -R nginx.nginx /apps/nginx
#修改权限
[root@localhost ~]#vim /usr/lib/systemd/system/nginx.service
#建立一个文件

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/apps/nginx/logs/nginx.pid
#注意文件位置,如果不对 启动不了
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
#注意启动文件位置
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target

[root@localhost ~]#systemctl daemon-reload
#重新加载
[root@localhost ~]#systemctl start nginx
#启动nginx
[root@localhost ~]#systemctl status nginx
#查看nginx状态
[root@localhost ~]#systemctl enable  nginx
#开机自启动

contrib代码高亮

[root@localhost ~]#cp -r /data/nginx-1.18.0/contrib/vim/*    /usr/share/vim/vimfiles/ 
[root@localhost ~]#vim /data/nginx-1.18.0/conf/nginx.conf

man帮助文档

[root@localhost ~]#cd /data
[root@localhost data]#ls
nginx-1.18.0  nginx-1.18.0.tar.gz
[root@localhost data]#cd nginx-1.18.0/
[root@localhost nginx-1.18.0]#ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@localhost nginx-1.18.0]#cd man
[root@localhost man]#ls
nginx.8
[root@localhost man]#man ./nginx.8 
#查看文档要加路径
[root@localhost man]#
[root@localhost man]#man /data/nginx-1.18.0/man/nginx.8 
#或者使用绝对路径

nginx四大文件

[root@localhost nginx-1.18.0]#cd /apps
[root@localhost apps]#ls
nginx
[root@localhost apps]#cd nginx/
[root@localhost nginx]#ls
conf  html  logs  sbin

  1. conf:保存nginx所有的配置文件,其中nginx.conf是nginx服务器的最核心最主要的配置文件,其他的.conf则是用来配置nginx相关的功能的,例如fastcgi功能使用的是fastcgi.conf和fastcgi_params两个文件,配置文件一般都有个样板配置文件,是文件名.default结尾,使用的使用将其复制为并将default去掉即可。
  2. html目录中保存了nginx服务器的web文件,但是可以更改为其他目录保存web文件,另外还有一个50x的web文件是默认的错误页面提示页面。
  3. logs:用来保存nginx服务器的访问日志错误日志等日志,logs目录可以放在其他路径,比如/var/logs/nginx里面。
  4. sbin:保存nginx二进制启动脚本,可以接受不同的参数以实现不同的功能。

nginx选项

[root@localhost nginx]#nginx -v
#nginx版本
[root@localhost nginx]#nginx -V
#编译了哪些模块

[root@localhost nginx]#nginx -t
#检测配置文件格式是否正确
[root@localhost nginx]#nginx -T
#打印配置文件

[root@localhost nginx]#nginx -c
#指明加载的配置文件
[root@localhost nginx]#nginx -g
#不使用配置文件里的配置,使用后面跟着的配置

[root@localhost ~]#nginx -g 'user mtwm;' 
#以mtwm身份运行,默认是以nginx身份

[root@localhost ~]#nginx -g 'daemon off;'    
#前台运行命令

[root@localhost nginx]#nginx -h
[root@localhost nginx]#nginx -?
#nginx帮助文件

[root@localhost nginx]#nginx -s
#发信号

[root@localhost ~]#man /data/nginx-1.18.0/man/nginx.8 
#可以通过man 用绝地路径查看nginx帮助手册
 -s signal 
                    stop    SIGTERM    直接停止
                    quit    SIGQUIT    优雅的退出:有人在访问不会结束进程
                    reopen  SIGUSR1    分割日志
                    reload  SIGHUP     重新加载配置文件


SIGNALS
     The master process of nginx can handle the following signals:

     SIGINT, SIGTERM  Shut down quickly.
     SIGHUP           Reload configuration, start the new worker process with a new configuration, and gracefully shut down old worker processes.
     SIGQUIT          Shut down gracefully.
     SIGUSR1          Reopen log files.
     SIGUSR2          Upgrade the nginx executable on the fly.
     SIGWINCH         Shut down worker processes gracefully.

     While there is no need to explicitly control worker processes normally, they support some signals too:

     SIGTERM          Shut down quickly.
     SIGQUIT          Shut down gracefully.
     SIGUSR1          Reopen log files.

分割日志
[root@localhost logs]#mv access.log access-24.log 
#把旧日志改名为access-24.log
[root@localhost logs]#touch access.log
#创建一个新日志access.log

打开nginx

实时查看新的日志,发现没有日志记录

实时查看旧的日志,发现日志还记录在旧日志中

再次打开nginx

#查看进程端口号
[root@localhost logs]#ps aux |grep nginx
#法一
[root@localhost logs]#cat /apps/nginx/logs/nginx.pid 
13007
#法二

#分割日志
[root@localhost logs]#kill -s USR1 13007
#需要进程号
[root@localhost logs]#nginx -s reopen
#不需要进程号

新日志中开始记录

旧日志中没有新记录计入

优雅退出
#服务器7-1
[root@localhost ~]#cd /apps/nginx/
[root@localhost nginx]#ls
client_body_temp  fastcgi_temp  logs        sbin       uwsgi_temp
conf              html          proxy_temp  scgi_temp
[root@localhost nginx]#cd html
[root@localhost html]#ls
50x.html  index.html
[root@localhost html]#dd if=/dev/zero of=/apps/nginx/html/m.img bs=1G count=3
[root@localhost html]#pstree -p |grep nginx
           |-nginx(13007)---nginx(13008)
[root@localhost html]#nginx -s quit
[root@localhost html]#
[root@localhost html]#pstree -p |grep nginx
           |-nginx(13007)---nginx(13008)
[root@localhost html]#
[root@localhost html]#pstree -p |grep nginx
[root@localhost html]#


#服务器7-3
[root@localhost ~]#cd /data
[root@localhost data]#ls
[root@localhost data]#wget --limit-rate=1M http://192.168.174.100/m.img
--2023-08-24 17:29:18--  http://192.168.174.100/m.img
正在连接 192.168.174.100:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:3221225472 (3.0G) [application/octet-stream]
正在保存至: “m.img”

 1% [                                    ] 60,596,544  1.00MB/s 剩余 50m 15s^C
[root@localhost data]#

飞行升级
#nginx -s reload  重新加载
[root@localhost html]#systemctl start nginx
[root@localhost html]#pstree -p |grep nginx
           |-nginx(14104)---nginx(14105)
[root@localhost html]#cat /apps/nginx//conf/nginx.conf |head -n 3

#user  nobody;
worker_processes  1;
[root@localhost html]#vim /apps/nginx//conf/nginx.conf
[root@localhost html]#cat /apps/nginx//conf/nginx.conf |head -n 3

#user  nobody;
worker_processes  2;
[root@localhost html]#nginx -s reload
[root@localhost html]#pstree -p |grep nginx
           |-nginx(14104)-+-nginx(14135)
           |              `-nginx(14136)
[root@localhost html]#

[root@localhost ~]#curl -I 192.168.174.100
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Fri, 25 Aug 2023 07:55:01 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 24 Aug 2023 07:52:10 GMT
Connection: keep-alive
ETag: "64e70c2a-264"
Accept-Ranges: bytes

[root@localhost usr]#ps aux |grep nginx

[root@localhost usr]#wget https://nginx.org/download/nginx-1.20.2.tar.gz -P /usr/local/src/
#下载nginx1.20.2包到/usr/local/src

[root@localhost src]#cd /usr/local/src
[root@localhost src]#ls
nginx-1.20.2.tar.gz
[root@localhost src]#tar xf nginx-1.20.2.tar.gz 
[root@localhost src]#ls
nginx-1.20.2  nginx-1.20.2.tar.gz
[root@localhost src]#cd nginx-1.20.2/
[root@localhost nginx-1.20.2]#ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@localhost nginx-1.20.2]#nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@localhost nginx-1.20.2]#./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module


[root@localhost nginx-1.20.2]#make -j2
#####不要make install

[root@localhost nginx-1.20.2]#ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src
[root@localhost nginx-1.20.2]#cd objs/
[root@localhost objs]#ls
autoconf.err  Makefile  nginx  nginx.8  ngx_auto_config.h  ngx_auto_headers.h  ngx_modules.c  ngx_modules.o  src
[root@localhost objs]#./nginx -v
nginx version: nginx/1.20.2

[root@localhost objs]#cd /apps/nginx/sbin/
[root@localhost sbin]#ls
nginx
[root@localhost sbin]#mv nginx nginx.bak
[root@localhost sbin]#ls
nginx.bak
#把nginx1.18.0改名成nginx.bak

[root@localhost objs]#cp nginx /apps/nginx/sbin/
[root@localhost objs]#cd /apps/nginx/sbin/
[root@localhost sbin]#ls
nginx  nginx.bak
#将nginx1.20.2拷贝到/apps/nginx/sbin/

[root@localhost sbin]#cat /apps/nginx/logs/nginx.pid 
1137
[root@localhost sbin]#kill -USR2 1137
[root@localhost sbin]#
[root@localhost sbin]#ps aux |grep nginx

[root@localhost sbin]#cd /apps/nginx/logs/
[root@localhost logs]#ls
access-24.log  access.log  error.log  nginx.pid  nginx.pid.oldbin
[root@localhost logs]#cat nginx.pid.oldbin 
1137
[root@localhost logs]#cat nginx.pid
6056
[root@localhost logs]#kill -WINCH 1137
[root@localhost logs]#ps aux |grep nginx

回滚
[root@localhost logs]#kill -HUP `cat /apps/nginx/logs/nginx.pid.oldbin`
[root@localhost logs]#ps aux |grep nginx

[root@localhost logs]#kill -QUIT `cat /apps/nginx/logs/nginx.pid`  
[root@localhost logs]#ps aux |grep nginx

配置详细解释

修改worker数量
[root@localhost ~]#vim /apps/nginx/conf/nginx.conf
worker_processes  auto;
[root@localhost ~]#lscpu |grep -i cpu
[root@localhost ~]#nginx -s reload
[root@localhost ~]#ps aux |grep -v grep |grep nginx

关闭版本或修改版本

[root@localhost ~]#vim /apps/nginx/conf/nginx.conf


http {
    include       mime.types;
    server_tokens  off;
[root@localhost ~]#nginx -t
[root@localhost ~]#nginx -s reload

mime
[root@localhost conf]#cat /apps/nginx/conf/mime.types | head  

types {
    text/html                                        html htm shtml;
    text/css                                         css;
    text/xml                                         xml;
    image/gif                                        gif;
    image/jpeg                                       jpeg jpg;
    application/javascript                           js;
    application/atom+xml                             atom;
    application/rss+xml                              rss;

server下的root
root路径格式 指定文件的路径    url  
Syntax:	root path;
Default:	
root html;
Context:	http, server, location, 
指明软件的根目录
server块构建虚拟主机 实际只有一台 好像很多主机
[root@localhost ~]#vim /apps/nginx/conf/nginx.conf

    include       /apps/nginx/conf.d*.conf;

[root@localhost ~]#mkdir -p /apps/nginx/conf.d/

[root@localhost ~]#cd /apps/nginx/conf.d

[root@localhost conf.d]#vim pc.conf

server{
    listen 80;
    server_name www.pc.com;
    root /data/html/pc;

}

[root@localhost conf.d]#cp pc.conf m.conf
[root@localhost conf.d]#vim m.conf 

server{
    listen 80;
    server_name www.m.com;
    root /data/html/m;

}


[root@localhost conf.d]#cd /data

[root@localhost data]#mkdir -p html/m 
[root@localhost data]#mkdir -p html/pc 

[root@localhost data]#ls
404.html  favicon.ico  html  nginx-1.18.0  nginx-1.18.0.tar.gz
[root@localhost data]#cd html
[root@localhost html]#ls
m  pc


[root@localhost html]#cd m
[root@localhost m]#echo mmmmmm > index.html
[root@localhost m]#cat index.html 
mmmmmm

[root@localhost html]#cd ../pc
[root@localhost pc]#echo pcpcpcpcpcpc > index.html
[root@localhost pc]#cat index.html 
pcpcpcpcpcpc

[root@localhost html]#nginx -s reload


[root@localhost ~]#vim /etc/hosts

192.168.174.100 www.pc.com  www.m.com

[root@localhost ~]#curl www.pc.com
pcpcpcpcpcpc

[root@localhost ~]#curl www.m.com
mmmmmm


[root@localhost ~]#vim /apps/nginx/conf.d/m.conf 

server{
    listen 88;
    server_name www.m.com;
    root /data/html/m;

}


[root@localhost ~]#curl 192.168.174.100
pcpcpcpcpcpc
[root@localhost ~]#curl 192.168.174.100:88
mmmmmm

alias 别名
[root@localhost ~]#vim /apps/nginx/conf.d/pc.conf

server{
    listen 80;
    server_name www.pc.com;
    location /mdl {
     root /data/html;
     #相当于追加  将 文件夹mdl追加到/data/html/mdl/index.html
    }
}
[root@localhost ~]#cd /data/html/
[root@localhost html]#ls
m  pc
[root@localhost html]#mkdir mdl
[root@localhost html]#echo mdlhb > mdl/index.html
[root@localhost html]#cat mdl/index.html 
mdlhb
[root@localhost html]#nginx -s reload



[root@localhost ~]#vim /apps/nginx/conf.d/pc.conf

server{
    listen 80;
    server_name www.pc.com;
    location /mdl {
     root /data/html;
     #相当于追加  将 文件夹mdl追加到/data/html/mdl/index.html
    }
    location /hbw {
     alias /data/html;
     #相当于等价替换 /hbw=/data/html
    }

    
}
[root@localhost ~]#cd /data/html/
[root@localhost html]#ls
m mdl pc  
[root@localhost html]#echo hbwnb >> index.html
[root@localhost html]#cat index.html 
hbwnb
[root@localhost html]#ls
index.html  m  mdl  pc

[root@localhost html]#nginx -s reload


location
404
[root@localhost data]#vim /apps/nginx/conf/nginx.conf

        error_page  404              /404.html;

        location = /404.html {
              root  /data/;
        }

[root@localhost data]#vim 404.html
[root@localhost data]#cat 404.html
please call 10010 to solve the problem

[root@localhost data]#nginx -s reload

自定义图标
[root@localhost ~]#wget www.jd.com/favicon.ico

[root@localhost ~]#cp favicon.ico /data/

[root@localhost ~]#cd /data

[root@localhost data]#ls
favicon.ico  nginx-1.18.0  nginx-1.18.0.tar.gz

[root@localhost data]#vim /apps/nginx/conf/nginx.conf

        location = /favicon.ico {
              root  /data/;
        }


[root@localhost data]#nginx -s reload

标签:nginx,data,Nginx,html,conf,root,localhost
From: https://www.cnblogs.com/mtwm/p/17650369.html

相关文章

  • nginx从入门到实战
    概述异步非阻塞的高性能HTTP和反向代理服务器  nginx的运行架构 1、Master进程 启动检查nginx.conf是否正确 根据配置文件创建、监控worker进程的数量和状态 监听socket,接受client发起的请求,然后worker竞争抢夺连接,获胜的worker可以相应请求 接收管理员发送......
  • Nginx配置详细解释
    一、配置解释nginx官方帮助文档:http://nginx.org/en/docs/tengine帮助文档:http://tengine.taobao.org/nginx_docs/cn/docs/Nginx的配置文件的组成部分:主配置文件:nginx.conf子配置文件:includeconf.d/*.conf1.1全局配置nginx有多种模块核心模块:是Nginx服务器正常......
  • 安装及使用Nginx
    一、安装及使用Nginx1编译安装nginxcontrib:vim格式文件,修改nginx配置文件的格式,高亮cp-r/opt/nginx-1.18.0/contrib/vim/*/usr/share/vim/vimfiles/conf:配置文件man:man帮助manman/nginx.8不加路径看不了nginx.8文件src:源码包点c点h结尾的文件findsrc......
  • 2nginx平滑升级及信号使用
    nginx平滑升级及信号使用1信号nginx命令支持向其发送信号,实现不同功能nginx当做单独命令使用有以下选项[root@localhost~]#nginx-h 显示帮助信息nginxversion:nginx/1.18.0Usage:nginx[-?hvVtTq][-ssignal][-cfilename][-pprefix][-gdirectives]​​......
  • nginx部署vue项目报404问题
    解决刷新路由404问题这是因为我的vue项目是采用了history路由模式,又因为vue是单页面应用,一旦切换路由之后,之前的页面就会不存在,然后你再刷新回到之前的页面就会出现404了。(个人理解)解决的方法:将路由模式修改为hash模式(不建议,hash模式#太丑)修改nginx配置location/{tr......
  • 关于Kubernetes-v1.23.6-集群测试-创建一个nginx的deployment进行验证
    关于k8s集群环境搭建完成后,我们可以通过创建一个deployment进行效果的测试这里以nginx为例,还是在k8s-master上进行创建kubectlcreatedeploymentnginx--image=nginxkubectlexposedeploymentnginx--port=80--type=NodePort这里--port只是指定了容器(container )暴......
  • Nginx配置站点502维护页面
    在站点后端服务如果宕掉或在重启的时候,用户访问网站时,nginx引导跳转到网站维护页面。  为了实现这个功能,需要在Nginx配置中做两件事情:定义一个备用的错误页面:为Nginx设置一个备用页面,当后端服务不可用时,它将显示这个页面。使用proxy_intercept_errors指令:使......
  • 切割指定时间范围内的nginx日志
    切割指定时间范围内的nginx日志cataccess.log|awk-F"[|/]"'$6>="2017:19:00:00"&&$6<="2017:20:00:00"{print$0}'说明:-F域分隔符,这里指定了[]里面的内容,取/后面的内容$6表示日志里面的时间&&并且关系$0取出结果......
  • ajax完美解决跨域问题(jsonp、nginx反向代理)
    做过web前端人都知道,经常会有ajax跨域问题,下面列举我经常使用的解决办法第一种:使用jsonp,jquery的ajax方法支持jsonp,但是最大的缺点就是只支持get方式,而且服务端也要修改客户端test.html代码<!DOCTYPEhtml><html><head> <title>工作端</title> <metaname="viewport"content=......
  • 解决Nginx SSL 代理 Tomcat 获取 Scheme 总是 Http 问题
    背景公司之前用的是http,但是出于苹果app审核和服务器安全性问题,要改为https,我们公司用的是沃通的ssl,按照沃通的官方文档提供的步骤完成服务器的配置。架构上使用了Nginx+tomcat集群,且nginx下配置了SSL,tomcat没有配置SSL,项目使用https协议。原因配置成功后明明是https......