您当前的位置:首页 > 网站建设 > 网站维护
| php | asp | css | H5 | javascript | Mysql | Dreamweaver | Delphi | 网站维护 | 帝国cms | React | 考试系统 | ajax | jQuery |

Ubuntu下搭建与配置Nginx服务

51自学网 2022-07-22 18:47:03
  网站维护

一、Nginx

Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。

三大WEB服务器:apache, Nginx, lighttpd之一。在高连接并发的情况下,Nginx是Apache服务器不错的替代品。

nginx应用场合

  • 静态服务器。(图片,视频服务)另一个是lighttpd。并发几万,html,js,css,flv,jpg,gif等。
  • 动态服务,nginx——fastcgi 的方式运行PHP,jsp。(PHP并发在500-1500,MySQL 并发在300-1500)。
  • 反向代理,负载均衡。日pv2000W以下,都可以直接用nginx做代理。
  • 缓存服务。类似 SQUID,VARNISH。

官方网址:http://nginx.org/en/download.html

官网提供三种版本:

Nginx官网提供了三个类型的版本 
Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以说是开发版 
Stable version:最新稳定版,生产环境上建议使用的版本 
Legacy versions:遗留的老版本的稳定版

二、nginx服务搭建 

1、使用apt安装

sudo apt install nginx

2、安装后的位置: 

  • /usr/sbin/nginx:主程序
  • /etc/nginx:存放配置文件
  • /usr/share/nginx:存放静态文件
  • /var/log/nginx:存放日志

3、启动并验证效果

service nginx start  # 启动nginxservice nginx reload  # 重新加载nginx配置文件

在浏览器输入你的ip地址,如果出现Wellcome to nginx 那么就是配置成功。

另外两个命令

nginx -s reopen            # 重启 Nginxnginx -s stop              # 停止 Nginx

4、查看版本号:

~$ nginx -v nginx version: nginx/1.14.0 (Ubuntu)

三、nginx配置文件介绍

1、nginx 文件结构

  • 全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
  • events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
  • http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
  • server块:配置虚拟主机的相关参数,一个http中可以有多个server。
  • location块:配置请求的路由,以及各种页面的处理情况。
...              # 全局块。配置影响nginx全局的指令。events {         # events块。配置影响nginx服务器或与用户的网络连接。   ...}http      # http块。可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。{    ...   # http全局块    server        # server块。配置虚拟主机的相关参数,一个http中可以有多个server。     {         ...       # server全局块        location [PATTERN]   # location块。配置请求的路由,以及各种页面的处理情况。        {            ...        }        location [PATTERN]         {            ...        }    }    server    {      ...    }    ...     # http全局块}

2、默认的配置

### You should look at the following URL's in order to grasp a solid understanding# of Nginx configuration files in order to fully unleash the power of Nginx.# https://www.nginx.com/resources/wiki/start/# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/# https://wiki.debian.org/Nginx/DirectoryStructure## In most cases, administrators will remove this file from sites-enabled/ and# leave it as reference inside of sites-available where it will continue to be# updated by the nginx packaging team.## This file will automatically load configuration files provided by other# applications, such as Drupal or Wordpress. These applications will be made# available underneath a path with that package name, such as /drupal8.## Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.### Default server configuration#server {    listen 80 default_server;    listen [::]:80 default_server;    # SSL configuration    #    # listen 443 ssl default_server;    # listen [::]:443 ssl default_server;    #    # Note: You should disable gzip for SSL traffic.    # See: https://bugs.debian.org/773332    #    # Read up on ssl_ciphers to ensure a secure configuration.    # See: https://bugs.debian.org/765782    #    # Self signed certs generated by the ssl-cert package    # Don't use them in a production server!    #    # include snippets/snakeoil.conf;    root /var/www/html;    # Add index.php to the list if you are using PHP    index index.html index.htm index.nginx-debian.html;    server_name _;    location / {        # First attempt to serve request as file, then        # as directory, then fall back to displaying a 404.        try_files $uri $uri/ =404;    }    # pass PHP scripts to FastCGI server    #    #location ~ /.php$ {    #    include snippets/fastcgi-php.conf;    #    #    # With php-fpm (or other unix sockets):    #    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;    #    # With php-cgi (or other tcp sockets):    #    fastcgi_pass 127.0.0.1:9000;    #}    # deny access to .htaccess files, if Apache's document root    # concurs with nginx's one    #    #location ~ //.ht {    #    deny all;    #}}# Virtual Host configuration for example.com## You can move that to a different file under sites-available/ and symlink that# to sites-enabled/ to enable it.##server {#    listen 80;#    listen [::]:80;##    server_name example.com;##    root /var/www/example.com;#    index index.html;##    location / {#        try_files $uri $uri/ =404;#    }#}

3、nginx的基本配置

########### 每个指令必须有分号结束。##################user administrator administrators;  #配置用户或者组,默认为nobody nobody。#worker_processes 2;  #允许生成的进程数,默认为1#pid /nginx/pid/nginx.pid;   #指定nginx进程运行文件存放地址error_log log/error.log debug;  #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emergevents {    accept_mutex on;   #设置网路连接序列化,防止惊群现象发生,默认为on    multi_accept on;  #设置一个进程是否同时接受多个网络连接,默认为off    #use epoll;      #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport    worker_connections  1024;    #最大连接数,默认为512}http {    include       mime.types;   #文件扩展名与文件类型映射表    default_type  application/octet-stream; #默认文件类型,默认为text/plain    #access_log off; #取消服务日志        log_format myFormat '$remote_addr
下载地址:
Kubernetes安装Jenkins的思路详解
VMware虚拟机的三种网络模式(桥接模式Bridged、地址转换模式NAT、仅主机模式Host-Only)详解
51自学网,即我要自学网,自学EXCEL、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。
京ICP备13026421号-1