栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Nginx安装和配置文件讲解

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Nginx安装和配置文件讲解

Nginx详解

1.Nginx安装和启动

1).安装2).启动nginx 2.配置文件解析问题:

1.nginx.pid打开失败或失效的解决办法

1.Nginx安装和启动 1).安装

配置好服务器yum源

1.上传nginx源码包
2.安装依赖包
  1).安装gcc环境
    yum install -y gcc-c++
  2).安装PCRE库,用于解析正则表达式
    yum install -y pcre pcre-devel
  3).SSL安全的套接字协议,用于http安全传输,也就是https
  	yum install -y openssl openssl-devel
  4).zlib压缩和解压缩依赖
  	yum install -y zlib zlib-devel
3.添加nginx用户
    useradd -s /sbin/nologin -M nginx
4.解压压缩包
    tar -xvf nginx-1.18.0.tar.gz -C /usr/local/
5.创建makefile文件
	./configure --user=nginx --group=nginx --prefix=/usr/local/nginx-1.18.0  --with-http_stub_status_module --with-http_ssl_module
6.编译安装
	make && make install
#参数解析:
  make是用来编译的,它从Makefile中读取指令,然后编译。
  make install是用来安装的,它也从Makefile中读取指令,安装到指定的位置。
  configure命令是用来检测你的安装平台的目标特征的。它定义了系统的各个方面,包括nginx的被允许使用的连接处理的方法,比如它会检测你是不是有CC或GCC,并不是需要CC或GCC,它是个shell脚本,执行结束时,它会创建一个Makefile文件。
#nginx的configure命令支持以下参数:
--prefix=path 定义一个目录,存放服务器上的文件,也就是nginx的安装目录。默认使用 /usr/local/nginx。
--with-http_stub_status_module 启用status网页(模块能够获取Nginx自上次启动以来的工作状态)
--with-http_ssl_module 支持ssl模块
#其它参数详见:
https://www.nginx.cn/install
2).启动nginx
启动:
/usr/local/nginx-1.18.0/sbin/nginx
/usr/local/nginx/sbin/nginx  -h  查看命令帮助
-v               查看nginx版本
-V               查看编译参数
-t               测试默认配置文件
-c				 为 Nginx 指定一个配置文件
停止:(如果还有用户请求未完成也会强制关闭)
/usr/local/nginx-1.18.0/sbin/nginx -s stop
停止:(直到用户请求关闭、没有新的请求才会停止服务。针对http请求)
/usr/local/nginx-1.18.0/sbin/nginx -s quit
重载(加载配置文件):
/usr/local/nginx-1.18.0/sbin/nginx -s reload

打开浏览器访问服务器ip+端口,显示nginx默认页即安装成功(如果不显示检查防火墙是否关闭或是否开启对应80端口)
注意:
1.如果在云服务器安装,则要开启对应端口
2.如果在虚拟机安装,开启端口或关闭防火墙
3.在mac或win安装,关闭防火墙

2.配置文件解析


nginx配置文件详细解析:https://www.runoob.com/w3cnote/nginx-setup-intro.html

[root@web nginx-1.18.0]# cat conf/nginx.conf 
#运行worker进程的用户或组,默认用户为nobody
#user  nobody;
worker_processes  2;  #worker进程的最大数量,一般不能超过cpu内核数

#制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;   #指定nginx进程运行文件存放地址


events {
    #事件驱动模型,默认epoll
    #use epoll;
    #每个worker允许客户端连接的最大连接数
    worker_connections  1024;
}


http {
    include       mime.types;   #文件扩展名与文件类型映射表
    default_type  application/octet-stream;   #默认文件类型,默认为text/plain
	
	#自定义日志格式
	#参考: https://cloud.tencent.com/developer/article/1504339?from=article.detail.1173152
    #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;  #
	
	#用于文件的传输,**打开表示文件传输的效率提升**。默认为off,可以在http块,server块,location块
    sendfile        on;
    #和sendfile一起使用,当数据包累积到一定大小再发送,会提升传输效率。
    #例:外卖小哥在外卖平台接单取餐,一种方式一次取很多个餐放到盒子里然后再去挨个送,另一种方式取一个餐去送一次,对于外卖小哥来说前者效率更高。
    #tcp_nopush     on;  
	
	##连接超时时间,可以在http,server,location块
	#设置keep-alive客户端连接在服务器端保持开启的超时值(默认75s);值为0会禁用keep-alive客户端连接;
	#当使用nginx作为反向代理时,为了支持长连接,需要做到两点: 从client到nginx的连接是长连接、从nginx到server的连接是长连接
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

	#一个server是一个虚拟主机
    server {
        listen       8081;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            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;
        }

        # 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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

上面是nginx的基本配置,需要注意的有以下几点:

1、几个常见配置项:

1.$remote_addr 与 KaTeX parse error: Double subscript at position 7: http_x_̲forwarded_for 用…remote_user :用来记录客户端用户名称;
3. t i m e l o c a l : 用 来 记 录 访 问 时 间 与 时 区 ; 4. time_local : 用来记录访问时间与时区; 4. timel​ocal:用来记录访问时间与时区;4.request : 用来记录请求的url与http协议;
5. s t a t u s : 用 来 记 录 请 求 状 态 ; 成 功 是 200 ; 6. status : 用来记录请求状态;成功是200; 6. status:用来记录请求状态;成功是200;6.body_bytes_s ent :记录发送给客户端文件主体内容大小;
7. h t t p r e f e r e r : 用 来 记 录 从 那 个 页 面 链 接 访 问 过 来 的 ; 8. http_referer :用来记录从那个页面链接访问过来的; 8. httpr​eferer:用来记录从那个页面链接访问过来的;8.http_user_agent :记录客户端浏览器的相关信息;
2、惊群现象:一个网路连接到来,多个睡眠的进程被同时叫醒,但只有一个进程能获得链接,这样会影响系统性能。

3、每个指令必须有分号结束。

原文地址:https://www.cnblogs.com/knowledgesea/p/5175711.html

问题: 1.nginx.pid打开失败或失效的解决办法

创建缺少的目录

启动nginx指定配置文件启动: ./nginx -c /usr/local/nginx-1.18.0/conf/nginx.conf

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/752622.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号