Linux服务器安装Nginx

安装包可自行前往官网下载,非常小,下载快
http://nginx.org/

一、安装

1、下载tar.gz文件,上传至服务器

2、tar -zxvf nginx-1.18.0.tar.gz
在这里插入图片描述

3、cd nginx-1.18.0/
ll
./configure
make
make install
在这里插入图片描述

4、whereis nginx 查看位置
在这里插入图片描述

5、进入位置启动
./nginx启动
在这里插入图片描述

6、启动成功后不会有任何提示
cd … 后退到Nginx目录下
[root@VM-4-3-centos sbin]# cd …
[root@VM-4-3-centos nginx]# cd conf/
[root@VM-4-3-centos conf]# cat nginx.conf
看到端口为80
在这里插入图片描述

7、网址登陆 服务器地址:80
在这里插入图片描述

如图显示,则安装成功

二、Nginx常用命令

1、查看 nginx 版本号
./nginx -v
2、启动 nginx
./nginx
3、停止 nginx
./nginx -s stop
4、重新加载 nginx 敲黑板,使用频率非常高
./nginx -s reload

三、nginx.conf配置文件

直接whereis nginx 然后进入路径
cd config
vim/vi nginx.conf

配置文件中的内容(包含三部分)
(1)全局块:配置服务器整体运行的配置指令
从配置文件开始到 events 块之间的内容,主要会设置一些影响 nginx 服务器整体运行的配置指令,主要包括配置运行 Nginx 服务器的用户(组)、允许生成的 worker process 数,进程 PID 存放路径、日志存放路径和类型以及配置文件的引入等。

在这里插入图片描述
比如上面第一行配置的:

这是 Nginx 服务器并发处理服务的关键配置,worker_processes 值越大,可以支持的并发处理量也越多,但是会受到硬件、软件等设备的制约
(2)events 块:影响 Nginx 服务器与用户的网络连接
events 块涉及的指令主要影响 Nginx 服务器与用户的网络连接,常用的设置包括是否开启对多 work process下的网络连接进行序列化,是否允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个 wordprocess 可以同时支持的最大连接数等(并发数)。

上述例子就表示每个 work process 支持的最大连接数为 1024.这部分的配置对 Nginx 的性能影响较大,在实际中应该灵活配置。
在这里插入图片描述

(3)http 块
这算是 Nginx 服务器配置中最频繁的部分,代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。需要注意的是:http 块也可以包括 http 全局块、server 块。
在这里插入图片描述
①、http 全局块
http 全局块配置的指令包括文件引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等。
在这里插入图片描述

②、server 块
这块和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是完全一样的,该技术的产生是为了节省互联网服务器硬件成本。

每个 http 块可以包括多个 server 块,而每个 server 块就相当于一个虚拟主机。而每个 server 块也分为全局 server 块,以及可以同时包含多个 locaton 块。
在这里插入图片描述

全局 server 块
最常见的配置是本虚拟机主机的监听配置和本虚拟主机的名称或 IP 配置。

location 块
一个 server 块可以配置多个 location 块。这块的主要作用是基于 Nginx 服务器接收到的请求字符串(例如 server_name/uri-string),对虚拟主机名称(也可以是 IP 别名)之外的字符串(例如 前面的 /uri-string)进行匹配,对特定的请求进行处理。地址定向、数据缓存和应答控制等功能,还有许多第三方模块的配置也在这里进行。

Nginx.conf配置文件详细说明(附备注)

########   Nginxmain(全局配置)文件
#指定nginx运行的用户及用户组,默认为nobody
#user  nobody;

#开启的线程数,一般跟逻辑CPU核数一致
worker_processes  1;

#定位全局错误日志文件,级别以notice显示,还有debug,info,warn,error,crit模式,debug输出最多,crir输出最少,根据实际环境而定
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#指定进程id的存储文件位置
#pid        logs/nginx.pid;

#指定一个nginx进程打开的最多文件描述符数目,受系统进程的最大打开文件数量限制
#worker_rlimit_nofile 65535

events {
    #设置工作模式为epoll,除此之外还有select,poll,kqueue,rtsig和/dev/poll模式
    #use epoll;

    #定义每个进程的最大连接数,受系统进程的最大打开文件数量限制。
    worker_connections  1024;
}

#######NginxHttp服务器配置,Gzip配置
http {
    #主模块指令,实现对配置文件所包含的文件的设定,可以减少主配置文件的复杂度,DNS主配置文件中的zonerfc1912,acl基本上都是用include语句。
    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"';

    #引用日志main
    #access_log  logs/access.log  main;

    #设置允许客户端请求的最大的单个文件字节数
    #client_max_body_size 20M;
    #指定来自客户端请求头的headebuffer大小
    #client_header_buffer_size  32k;
    #指定连接请求试图写入缓存文件的目录路径
    #client_body_temp_path /dev/shm/client_body_temp;
    #指定客户端请求中较大的消息头的缓存最大数量和大小,目前设置为432KB
    #large client_header_buffers 4 32k;

    #开启高效文件传输模式
    sendfile        on;
    #开启防止网络阻塞
    #tcp_nopush     on;
    #开启防止网络阻塞
    #tcp_nodelay    on;

    #设置客户端连接保存活动的超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #设置客户端请求读取超时时间
    #client_header_timeout 10;
    #设置客户端请求主体读取超时时间
    #client_body_timeout 10;
    #用于设置相应客户端的超时时间
    #send_timeout

    ####HttpGZip模块配置
    #httpGzip modules
    #开启gzip压缩
    #gzip  on;
    #设置允许压缩的页面最小字节数
    #gzip_min_length 1k;
    #申请4个单位为16K的内存作为压缩结果流缓存
    #gzip_buffers 4 16k;
    #设置识别http协议的版本,默认为1.1
    #gzip_http_version 1.1;
    #指定gzip压缩比,1-9数字越小,压缩比越小,速度越快
    #gzip_comp_level 2;
    #指定压缩的类型
    #gzip_types text/plain application/x-javascript text/css application/xml;
    #让前端的缓存服务器进过gzip压缩的页面
    #gzip_vary on;

    #########Nginx的server虚拟主机配置
    server {
        #监听端口为 80
        listen       80;

        #设置主机域名
        server_name  localhost;

        #设置访问的语言编码
        #charset koi8-r;

        #设置虚拟主机访问日志的存放路径及日志的格式为main
        #access_log  logs/host.access.log  main;

        #设置虚拟主机的基本信息
        location / {
            #设置虚拟主机的网站根目录
            root   html;

            #设置虚拟主机默认访问的网页
            index  index.html index.htm;
        }
		#javaboystudy自定义名称,负载均衡
		upstream javaboystudy{
		#weight权重,如下:访问四次,有三次进入192.168.37.136:8080
		server 192.168.37.136:8080 weight=3;
		server 192.168.37.136:8080 weight=1;
		}
		
		
}

实战演示案例

1 #user  nobody;
  2 worker_processes  1;
  3 #error_log  logs/error.log;
  4 #error_log  logs/error.log  notice;
  5 #error_log  logs/error.log  info;
  7 #pid        logs/nginx.pid;
 10 events {
 11     worker_connections  1024;
 12 }
 15 http {
 16     include       mime.types;
 17     default_type  application/octet-stream;
 18 
 19     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 20     #                  '$status $body_bytes_sent "$http_referer" '
 21     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 22 
 23     #access_log  logs/access.log  main;
 24 
 25     sendfile        on;
 26     #tcp_nopush     on;
 27 
 28     #keepalive_timeout  0;
 29     keepalive_timeout  65;
 30 
 31     #gzip  on;
 32     
 33     #配置tomcat的IP地址和访问端口
 34     upstream gw {
 35         server 192.168.37.136:8080 weight=1;    
 36     }
 37     server {
 38         listen       80;
 39         server_name  localhost;
 40 
 41         #charset koi8-r;
 42 
 43         #access_log  logs/host.access.log  main;
 44 
 45         location / {
 46             root   html;
 47             index  index.html index.htm;
 48         }
 49     #Nginx代理配置
 50     location /lywh {
 51         proxy_pass http://gw/lywh;
 52     }
 53     location /sapi {
 54         proxy_pass http://gw/shopappapi;
 55     }
 56     location /cas{
 57         proxy_pass http://gw/cas-server-webapp-4.0.0/login;
 58     }
 59     location /doc{
 60         proxy_pass http://gw/docs;
 61     }
 62 
 63     #error_page  404              /404.html;
 64 
 65         # redirect server error pages to the static page /50x.html
 66         #
 67         error_page   500 502 503 504  /50x.html;
 68         location = /50x.html {
 69             root   html;
 70         }
 71 
 72         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 73         #
 74         #location ~ \.php$ {
 75         #    proxy_pass   http://127.0.0.1;
 76         #}
 77 
 78         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 79         #
 80         #location ~ \.php$ {
 81         #    root           html;
 82         #    fastcgi_pass   127.0.0.1:9000;
 83         #    fastcgi_index  index.php;
 84         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 85         #    include        fastcgi_params;
 86         #}
 87 
 88         # deny access to .htaccess files, if Apache's document root
 89         # concurs with nginx's one
 90         #
 91         #location ~ /\.ht {
 92         #    deny  all;
 93         #}
 94     }
 95 
 96 
 97     # another virtual host using mix of IP-, name-, and port-based configuration
 98     #
 99     #server {
100     #    listen       8000;
101     #    listen       somename:8080;
102     #    server_name  somename  alias  another.alias;
103 
104     #    location / {
105     #        root   html;
106     #        index  index.html index.htm;
107     #    }
108     #}
109 
110 
111     # HTTPS server
112     #
113     #server {
114     #    listen       443 ssl;
115     #    server_name  localhost;
116 
117     #    ssl_certificate      cert.pem;
118     #    ssl_certificate_key  cert.key;
119 
120     #    ssl_session_cache    shared:SSL:1m;
121     #    ssl_session_timeout  5m;
122 
123     #    ssl_ciphers  HIGH:!aNULL:!MD5;
124     #    ssl_prefer_server_ciphers  on;
125 
126     #    location / {
127     #        root   html;
128     #        index  index.html index.htm;
129     #    }
130     #}
131 
132 }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值