1. 首页
  2. 技术知识

Ubuntu下搭建与配置Nginx服务

目录

    一、Nginx

      nginx应用场合

    二、nginx服务搭建

      1、使用apt安装2、安装后的位置: 3、启动并验证效果4、查看版本号:

    三、nginx配置文件介绍

      1、nginx 文件结构2、默认的配置3、nginx的基本配置

    四、 nginx虚拟主机配置

      验证配置文件:

    五、nginx全局变量六、Nginx主要配置

      1、静态Http服务器配置2、反向X服务器配置3、负载均衡配置4、虚拟主机配置

一、Nginx

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

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

nginx应用场合

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

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

官网提供三种版本:

Nginx官网提供了三个类型的版本

Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以说是开发版

Stable version:最新稳定版,生产环境上建议使用的版本

Legacy versions:遗留的老版本的稳定版

二、nginx服务搭建


1、使用apt安装

  1. sudo apt install nginx

复制代码
2、安装后的位置:

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


3、启动并验证效果

  1. service nginx start  # 启动nginx
  2. service nginx reload  # 重新加载nginx配置文件

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

另外两个命令

  1. nginx -s reopen            # 重启 Nginx
  2. nginx -s stop              # 停止 Nginx

复制代码


4、查看版本号:

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

复制代码
三、nginx配置文件介绍


1、nginx 文件结构

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

  1. …              # 全局块。配置影响nginx全局的指令。
  2. events {         # events块。配置影响nginx服务器或与用户的网络连接。
  3.    …
  4. }
  5. http      # http块。可以嵌套多个server,配置X,缓存,日志定义等绝大多数功能和第三方模块的配置。
  6. {
  7.     …   # http全局块
  8.     server        # server块。配置虚拟主机的相关参数,一个http中可以有多个server。
  9.     {
  10.         …       # server全局块
  11.         location [PATTERN]   # location块。配置请求的路由,以及各种页面的处理情况。
  12.         {
  13.             …
  14.         }
  15.         location [PATTERN]
  16.         {
  17.             …
  18.         }
  19.     }
  20.     server
  21.     {
  22.       …
  23.     }
  24.     …     # http全局块
  25. }

复制代码
2、默认的配置

  1. ##
  2. # You should look at the following URL’s in order to grasp a solid understanding
  3. # of Nginx configuration files in order to fully unleash the power of Nginx.
  4. # https://www.nginx.com/resources/wik1/start/
  5. # https://www.nginx.com/resources/wik1/start/topics/tutorials/config_pitfalls/
  6. # https://wik1.debian.org/Nginx/DirectoryStructure
  7. #
  8. # In most cases, administrators will remove this file from sites-enabled/ and
  9. # leave it as reference inside of sites-available where it will continue to be
  10. # updated by the nginx packaging team.
  11. #
  12. # This file will automatically load configuration files provided by other
  13. # APPlications, such as Drupal or WordPress. These applications will be made
  14. # available underneath a path with that package name, such as /drupal8.
  15. #
  16. # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
  17. ##
  18. # Default server configuration
  19. #
  20. server {
  21.     listen 80 default_server;
  22.     listen [::]:80 default_server;
  23.     # SSL configuration
  24.     #
  25.     # listen 443 ssl default_server;
  26.     # listen [::]:443 ssl default_server;
  27.     #
  28.     # Note: You should disable gzip for SSL traffic.
  29.     # See: https://bugs.debian.org/773332
  30.     #
  31.     # Read up on ssl_ciphers to ensure a secure configuration.
  32.     # See: https://bugs.debian.org/765782
  33.     #
  34.     # Self signed certs generated by the ssl-cert package
  35.     # Don’t use them in a production server!
  36.     #
  37.     # include snippets/snakeoil.conf;
  38.     root /var/www/html;
  39.     # Add index.php to the list if you are using PHP
  40.     index index.html index.htm index.nginx-debian.html;
  41.     server_name _;
  42.     location / {
  43.         # First attempt to serve request as file, then
  44.         # as directory, then fall back to displaying a 404.
  45.         try_files $uri $uri/ =404;
  46.     }
  47.     # pass PHP scripts to FastCGI server
  48.     #
  49.     #location ~ \.php$ {
  50.     #    include snippets/fastcgi-php.conf;
  51.     #
  52.     #    # With php-fpm (or other unix sockets):
  53.     #    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
  54.     #    # With php-cgi (or other tcp sockets):
  55.     #    fastcgi_pass 127.0.0.1:9000;
  56.     #}
  57.     # deny access to .htaccess files, if Apache’s document root
  58.     # concurs with nginx’s one
  59.     #
  60.     #location ~ /\.ht {
  61.     #    deny all;
  62.     #}
  63. }
  64. # Virtual Host configuration for example.com
  65. #
  66. # You can move that to a different file under sites-available/ and symlink that
  67. # to sites-enabled/ to enable it.
  68. #
  69. #server {
  70. #    listen 80;
  71. #    listen [::]:80;
  72. #
  73. #    server_name example.com;
  74. #
  75. #    root /var/www/example.com;
  76. #    index index.html;
  77. #
  78. #    location / {
  79. #        try_files $uri $uri/ =404;
  80. #    }
  81. #}

复制代码
3、nginx的基本配置

  1. ########### 每个指令必须有分号结束。#################
  2. #user administrator administrators;  #配置用户或者组,默认为nobody nobody。
  3. #worker_processes 2;  #允许生成的进程数,默认为1
  4. #pid /nginx/pid/nginx.pid;   #指定nginx进程运行文件存放地址
  5. error_log log/error.log debug;  #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
  6. events {
  7.     accept_mutex on;   #设置网路连接序列化,防止惊群现象发生,默认为on
  8.     multi_accept on;  #设置一个进程是否同时接受多个网络连接,默认为off
  9.     #use epoll;      #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
  10.     worker_connections  1024;    #最大连接数,默认为512
  11. }
  12. http {
  13.     include       mime.types;   #文件扩展名与文件类型映射表
  14.     default_type  application/octet-stream; #默认文件类型,默认为text/plain
  15.     #access_log off; #取消服务日志   
  16.     log_format myFormat ‘$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for’; #自定义格式
  17.     access_log log/access.log myFormat;  #combined为日志格式的默认值
  18.     sendfile on;   #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
  19.     sendfile_max_chunk 100k;  #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
  20.     keepalive_timeout 65;  #连接超时时间,默认为75s,可以在http,server,location块。
  21.     upstream mysvr {   
  22.       server 127.0.0.1:7878;
  23.       server 192.168.10.121:3333 backup;  #热备
  24.     }
  25.     error_page 404 https://www.baidu.com; #错误页
  26.     server {
  27.         keepalive_requests 120; #单连接请求上限次数。
  28.         listen       80;   #监听端口
  29.         server_name  127.0.0.1;   #监听地址      
  30.         location  ~*^.+$ {       #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
  31.            #root path;  #根目录
  32.            #index vv.txt;  #设置默认页
  33.            proxy_pass  http://mysvr;  #请求转向mysvr 定义的服务器列表
  34.            deny 127.0.0.1;  #拒绝的ip
  35.            allow 172.18.5.54; #允许的ip           
  36.         }
  37.     }
  38. }

复制代码
四、 nginx虚拟主机配置

  1. #下面是server虚拟主机的配置段
  2. server
  3.   {
  4.     listen 80;#监听端口
  5.     server_name localhost;#域名
  6.     index index.html index.htm index.php;
  7.     root /usr/local/webserver/nginx/html;#站点目录
  8.       location ~ .*\.(php|php5)?$
  9.     {
  10.       #fastcgi_pass unix:/tmp/php-cgi.sock;
  11.       fastcgi_pass 127.0.0.1:9000;
  12.       fastcgi_index index.php;
  13.       include fastcgi.conf;
  14.     }
  15.     location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
  16.     {
  17.       expires 30d;
  18.     #access_log off;
  19.     }
  20.     location ~ .*\.(js|css)?$
  21.     {
  22.       expires 15d;
  23.      #access_log off;
  24.     }
  25.     access_log off;
  26.   }

复制代码
验证配置文件:

  1. root@ubuntu: nginx -t
  2. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  3. nginx: configuration file /etc/nginx/nginx.conf test is successful

复制代码
五、nginx全局变量

    $remote_addr 与 $http_x_forwarded_for 用以记录客户端的ip地址;$remote_user :用来记录客户端用户名称;$time_local : 用来记录访问时间与时区;$request : 用来记录请求的url与http协议;$status : 用来记录请求状态;成功是200;$body_bytes_s ent :记录发送给客户端文件主体内容大小;$http_referer :用来记录从那个页面链接访问过来的;$http_user_agent :记录客户端浏览器的相关信息;

  1. 访问链接是:http://localhost:88/test1/test.php
  2. 网站路径是:/var/www/html
  3. $host:localhost
  4. $server_port:88
  5. $request_uri:<a href=”http://localhost:88/test1/test.php” rel=”external nofollow”   target=”_blank”>http:</a>//localhost:88/test1/test.php
  6. $document_uri:/test1/test.php
  7. $document_root:/var/www/html
  8. $request_filename:/var/www/html/test1/test.php

复制代码
六、Nginx主要配置


1、静态Http服务器配置

首先,Nginx是一个HTTP服务器,可以将服务器上的静态文件(如HTML、图片)通过HTTP协议展现给客户端。

配置:

  1. server {
  2.     listen 80;   # 端口
  3.     server_name localhost  192.168.1.100;   # 域名   
  4.     location / {             # 代表这是项目根目录
  5.         root /usr/share/nginx/www;   # 虚拟目录
  6.     }
  7. }

复制代码
2、反向X服务器配置

什么是反向X?

客户端本来可以直接通过HTTP协议访问某网站应用服务器,如果网站管理员在中间加上一个Nginx,客户端请求Nginx,Nginx请求应用服务器,然后将结果返回给客户端,此时Nginx就是反向X服务器。

反向X配置:

  1. server {
  2.     listen 80;
  3.     location / {
  4.         proxy_pass http://192.168.0.112:8080;   # 应用服务器HTTP地址
  5.     }
  6. }

复制代码 既然服务器可以直接HTTP访问,为什么要在中间加上一个反向X,不是多此一举吗?反向X有什么作用?继续往下看,下面的负载均衡、虚拟主机,都基于反向X实现,当然反向X的功能也不仅仅是这些。

3、负载均衡配置

当网站访问量非常大,也摊上事儿了。因为网站越来越慢,一台服务器已经不够用了。于是将相同的应用部署在多台服务器上,将大量用户的请求分配给多台机器处理。同时带来的好处是,其中一台服务器万一挂了,只要还有其他服务器正常运行,就不会影响用户使用。Nginx可以通过反向X来实现负载均衡。

负载均衡配置:

  1. upstream myapp {
  2.     server 192.168.0.111:8080;   # 应用服务器1
  3.     server 192.168.0.112:8080;   # 应用服务器2
  4. }
  5. server {
  6.     listen 80;
  7.     location / {
  8.         proxy_pass http://myweb;
  9.     }
  10. }

复制代码
4、虚拟主机配置

有的网站访问量大,需要负载均衡。然而并不是所有网站都如此出色,有的网站,由于访问量太小,需要节省成本,将多个网站部署在同一台服务器上。

例如将www.aaa.com和www.bbb.com两个网站部署在同一台服务器上,两个域名解析到同一个IP地址,但是用户通过两个域名却可以打开两个完全不同的网站,互相不影响,就像访问两个服务器一样,所以叫两个虚拟主机。

虚拟主机配置:

  1. server {
  2.     listen 80 default_server;
  3.     server_name _;
  4.     return 444;   # 过滤其他域名的请求,返回444状态码
  5. }
  6. server {
  7.     listen 80;
  8.     server_name www.aaa.com;   # www.aaa.com域名
  9.     location / {
  10.         proxy_pass http://localhost:8080;   # 对应端口号8080
  11.     }
  12. }
  13. server {
  14.     listen 80;
  15.     server_name www.bbb.com;   # www.bbb.com域名
  16.     location / {
  17.         proxy_pass http://localhost:8081;   # 对应端口号8081
  18.     }
  19. }

复制代码 在服务器8080和8081分别开了一个应用,客户端通过不同的域名访问,根据server_name可以反向X到对应的应用服务器。

虚拟主机的原理是通过HTTP请求头中的Host是否匹配server_name来实现的,有兴趣的同学可以研究一下HTTP协议。

另外,server_name配置还可以过滤有人恶意将某些域名指向你的主机服务器。

到此这篇关于Ubuntu下搭建与配置Nginx服务的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持软件技术网。

原创文章,作者:starterknow,如若转载,请注明出处:https://www.starterknow.com/109145.html

联系我们