Loading

Nginx服务器安装配置

环境:

Linux CentOS 7

3.10.0-1062.9.1.el7.x86_64

强调:server{} 包含在http{}内部,每一个server{}都是一个虚拟主机(站点)!!!

Nginx服务器安装配置

安装依赖

yum install pcre* -y
yum install openssl* -y

Nginx安装

  1. Nginx下载

     wget http://nginx.org/download/nginx-1.19.0.tar.gz
    
  2. 解压安装包

    tar -zxf nginx-1.19.0.tar.gz
    
  3. 检查配置文件是否生效

    进入解压目录,根据提示安装依赖

    ./configure
    
  4. 编译安装

    make -j4 && make install
    

Nginx服务器配置

nginx命令添加

创建软连接

ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

启动Nginx

启动nginx服务器,查看进程状态,通过ip访问测试

nginx # 启动nginx服务器

ps -aux|grep nginx # 查看nginx进程

配置服务器主配置文件

vi /usr/local/nginx/conf/nginx.conf

  1. 文件第一行添加:

    user	root;
    
  2. 删除server部分

  3. 在http括号内添加一行

    这是自定义nginx sever配置文件

    include /root/nginx/*.conf;
    

自定义服务器配置

编辑自定义nginx sever配置文件

vim /root/nginx/nginx-server.conf

server {
    listen 80; #监听IP端口 
    server_name localhost; #主机名
    location /{
        root /root/nginx/html;
        index  index.html index.htm; #定义首页索引文件的名称
    }
}

检查配置文件

nginx -t

提示如下:成功

[root@k8s-master nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

重启服务器

nginx -s reload 

测试

在配置文件中的根目录创建 index.html,随便输几个字母(eg: Nginx test...),保存退出。浏览器访问测试。

vim /root/nginx/html/index.html

Nginx配置文件注释

Nginx主配置

{% fold 点击显示/隐藏-Nginx主配置文件 %}

#user  nobody; 
user	root
#开启进程数 <=CPU数 
worker_processes 1; 
  
#错误日志保存位置 
#error_log logs/error.log; 
#error_log logs/error.log notice; 
#error_log logs/error.log info; 
  
#进程号保存文件 
#pid logs/nginx.pid; 
  
#等待事件 
events { 
  #每个进程最大连接数(最大连接=连接数x进程数)  
  worker_connections 1024; 
}
 
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压缩 
  #gzip on; 
    
  #设定请求缓冲 
  client_header_buffer_size 1k; 
  large_client_header_buffers 4 4k; 
    
  #设定负载均衡的服务器列表 
  upstream tomcat-servers {
    #weigth参数表示权值,权值越高被分配到的几率越大 
    #max_fails 当有#max_fails个请求失败,就表示后端的服务器不可用,默认为1,将其设置为0可以关闭检查 
    #fail_timeout 在以后的#fail_timeout时间内nginx不会再把请求发往已检查出标记为不可用的服务器 
    #这里指定多个源服务器,ip:端口,80端口的话可写可不写 
    server 127.0.0.1:8080 weight=5 max_fails=2 fail_timeout=600s; 
    #server 127.0.0.2:8080 weight=3 max_fails=2 fail_timeout=600s;  
  } 
    
    include /root/nginx/*conf;	# 添加自定义的配置文件!!!!
}

{% endfold %}

Nginx虚拟主机配置

{% fold 点击显示/隐藏-Nginx自定义虚拟主机配置 %}

server {
    listen       80;  //监听端口为80,可以自定义其他端口,也可以加上IP地址,如,listen 127.0.0.1:8080;
    server_name  localhost; //定义网站域名,可以写多个,用空格分隔
    #charset koi8-r; //定义网站的字符集,一般不设置,而是在网页代码中设置
    #access_log  logs/host.access.log  main; //定义访问日志,相当于局部变量 ,可以针对每一个server(即每一个站点)设置它们自己的访问日志
    ##在server{}里有很多location配置段
    #对本server"/"启用负载均衡
    location / {
        root   html;  //定义网站根目录,目录可以是相对路径也可以是绝对路径
        index  index.html index.htm; #定义首页索引文件的名称
        
        #以下是一些反向代理的配置可删除. 
        # proxy_redirect off;  
        # proxy_set_header Host $host;  
        # 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 90; #nginx跟后端服务器连接超时时间(代理连接超时)  
        # proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)  
        # proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)  
        # proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小  
        # proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置  
        # proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)  
        # proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传 
    }
    
    #设定查看Nginx状态的地址  
    location /NginxStatus {  
        stub_status on;  
        access_log off;  
        #allow 192.168.1.88; 
        #deny all; 
        #auth_basic "NginxStatus";  
        #auth_basic_user_file conf/htpasswd;  
    }
  
    #error_page  404              /404.html;  //定义404页面
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;  //当状态码为500、502、503、504时,则访问50x.html
    location = /50x.html {
        root   html;  //定义50x.html所在路径
    }
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #定义访问php脚本时,将会执行本location{}部分指令
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;  //proxy_pass后面指定要访问的url链接,用proxy_pass实现代理。
    #}
    # 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服务器监听端口与地址,支持两种形式,1 IP:Port, 2 unix:/path/to/sockt
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  //定义SCRIPT_FILENAME变量,后面的路径/scripts为上面的root指定的目录
    #    include        fastcgi_params; //引用prefix/conf/fastcgi_params文件,该文件定义了fastcgi相关的变量
    #}
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    # 
    #location ~ /\.ht {   //访问的url中,以/.ht开头的,如,www.example.com/.htaccess,会被拒绝,返回403状态码。
    #    deny  all;  //这里的all指的是所有的请求。
    #}
}

{% endfold %}

SSL配置

Nginx 默认安装的情况下ssl模块并未被安装,如果要使用该模块则需要在编译nginx时指定with-http_ssl_module参数。

  1. 查看ngixn版本极其编译参数

    /usr/local/nginx/sbin/nginx -V
    
  2. 进入nginx源码目录

  3. 重新编译模块

    ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module
    
  4. make 全完别 make install 覆盖安装

  5. 备份旧的nginx程序

    cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
    
  6. 将刚刚编译好的nginx覆盖掉原有的nginx(这个时候nginx要停止状态 --- kill 9

    cp ./objs/nginx /usr/local/nginx/sbin/
    
  7. 查看模块安装结果

    nginx -V
    ## or
    /usr/local/nginx/sbin/nginx -V
    
  8. 检查配置&启动nginx

参考

nginx服务器搭建以及配置

posted @ 2021-01-25 14:06  JakeLin  阅读(215)  评论(0)    收藏  举报