Ubuntu下搭建与配置Nginx服务

目录
  • 一、Nginx
    • nginx应用场合
  • 二、nginx服务搭建
    • 1、使用apt安装
    • 2、安装后的位置:
    • 3、启动并验证效果
    • 4、查看版本号:
  • 三、nginx配置文件介绍
    • 1、nginx 文件结构
    • 2、默认的配置
    • 3、nginx的基本配置
  • 四、 nginx虚拟主机配置
    • 验证配置文件:
  • 五、nginx全局变量
  • 六、Nginx主要配置
    • 1、静态Http服务器配置
    • 2、反向代理服务器配置
    • 3、负载均衡配置
    • 4、虚拟主机配置

一、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  # 启动nginx
service nginx reload  # 重新加载nginx配置文件

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

另外两个命令

nginx -s reopen            # 重启 Nginx
nginx -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|emerg
events {
    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–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
    access_log log/access.log myFormat;  #combined为日志格式的默认值
    sendfile on;   #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
    sendfile_max_chunk 100k;  #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
    keepalive_timeout 65;  #连接超时时间,默认为75s,可以在http,server,location块。

    upstream mysvr {
      server 127.0.0.1:7878;
      server 192.168.10.121:3333 backup;  #热备
    }
    error_page 404 https://www.baidu.com; #错误页
    server {
        keepalive_requests 120; #单连接请求上限次数。
        listen       80;   #监听端口
        server_name  127.0.0.1;   #监听地址
        location  ~*^.+$ {       #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
           #root path;  #根目录
           #index vv.txt;  #设置默认页
           proxy_pass  http://mysvr;  #请求转向mysvr 定义的服务器列表
           deny 127.0.0.1;  #拒绝的ip
           allow 172.18.5.54; #允许的ip
        }
    }
}

四、 nginx虚拟主机配置

#下面是server虚拟主机的配置段
 server
  {
    listen 80;#监听端口
    server_name localhost;#域名
    index index.html index.htm index.php;
    root /usr/local/webserver/nginx/html;#站点目录
      location ~ .*\.(php|php5)?$
    {
      #fastcgi_pass unix:/tmp/php-cgi.sock;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
    {
      expires 30d;
    #access_log off;
    }
    location ~ .*\.(js|css)?$
    {
      expires 15d;
     #access_log off;
    }
    access_log off;
  }

验证配置文件:

root@ubuntu: nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
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 :记录客户端浏览器的相关信息;
访问链接是:http://localhost:88/test1/test.php
网站路径是:/var/www/html

$host:localhost
$server_port:88
$request_uri:<a href="http://localhost:88/test1/test.php" rel="external nofollow"   target="_blank">http:</a>//localhost:88/test1/test.php
$document_uri:/test1/test.php
$document_root:/var/www/html
$request_filename:/var/www/html/test1/test.php

六、Nginx主要配置

1、静态Http服务器配置

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

server {
    listen 80;   # 端口
    server_name localhost  192.168.1.100;   # 域名
    location / {             # 代表这是项目根目录
        root /usr/share/nginx/www;   # 虚拟目录
    }
}

2、反向代理服务器配置

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

反向代理配置:

server {
    listen 80;
    location / {
        proxy_pass http://192.168.0.112:8080;   # 应用服务器HTTP地址
    }
}

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

3、负载均衡配置

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

负载均衡配置:

upstream myapp {
    server 192.168.0.111:8080;   # 应用服务器1
    server 192.168.0.112:8080;   # 应用服务器2
}
server {
    listen 80;
    location / {
        proxy_pass http://myweb;
    }
}

4、虚拟主机配置

有的网站访问量大,需要负载均衡。然而并不是所有网站都如此出色,有的网站,由于访问量太小,需要节省成本,将多个网站部署在同一台服务器上。 
例如将www.aaa.com和www.bbb.com两个网站部署在同一台服务器上,两个域名解析到同一个IP地址,但是用户通过两个域名却可以打开两个完全不同的网站,互相不影响,就像访问两个服务器一样,所以叫两个虚拟主机。

虚拟主机配置:

server {
    listen 80 default_server;
    server_name _;
    return 444;   # 过滤其他域名的请求,返回444状态码
}
server {
    listen 80;
    server_name www.aaa.com;   # www.aaa.com域名
    location / {
        proxy_pass http://localhost:8080;   # 对应端口号8080
    }
}
server {
    listen 80;
    server_name www.bbb.com;   # www.bbb.com域名
    location / {
        proxy_pass http://localhost:8081;   # 对应端口号8081
    }
}

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

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

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

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

(0)

相关推荐

  • Ubuntu系统搭建django+nginx+uwsgi的教程详解

    1. 在开发机上的准备工作 1.确认项目没有bug. 2.用pip freeze > requirements.txt将当前环境的包导出到requirements.txt文件中,方便在部署的时候安装. 3.将项目上传到服务器上的/srv目录下.这里以git的形式为例,打开终端,依次输入如下命令 •git init •git remote add origin xxx.git •git add . •git commit -m 'first commit' •git pull origin mas

  • Ubuntu使用nginx搭建webdav文件服务器的详细过程

    安装nginx 注意必须安装nginx-full, 默认的nginx里面并不包含webdav模块 $ sudo apt install -y nginx-full 创建相关文件夹 # 用来保存共享文件的位置 $ sudo mkdir -p /home/dav $ sudo chown -R www-data:www-data /home/dav 配置 $ sudo vim /etc/nginx/sites-enabled/webdav.conf 内容如下: server { listen 80;

  • ubuntu下搭建php开发环境(nginx+(cgi)php5fpm+memcached+xdebug)

    由于只是开发环境,所以都是选择比较简单的apt-get安装方式 ,但中间也遇到一点问题. 首先安装nginx nginx的安装和配置其实很简单,nginx本身非常轻量级, 直接 sudo apt-get install nginx 就可以了 他的配置文件是在/etc/nginx/里面,网站项目路径是在/var/www里面,安装之后一定要确保nginx的启动账户有对网站目录的访问权限,否则会报错. 第二步,安装cgi linux底下的cgi程序非常多,在这里我选择的是php5-fpm 方便  在这

  • Ubuntu中搭建Nginx、PHP环境最简单的方法

    前言:百度出来的结果好坑爹,而且某些文章说别人坑爹,可他自己也坑爹.求业界良心啊.还是谷歌靠谱. 系统环境:Ubuntu 13 和 Linux Mint 15都通过. 默认安装的是nginx 1.2.5,php5.4.9 先安装: 复制代码 代码如下: sudo apt-get install nginx php5-fpm 我是在新安装的Ubuntu13上测试通过的,真的只安装这两个东西就够了. 然后编辑配置文件. 复制代码 代码如下: sudo gedit /etc/nginx/site-av

  • Ubuntu下搭建与配置Nginx服务

    目录 一.Nginx nginx应用场合 二.nginx服务搭建 1.使用apt安装 2.安装后的位置: 3.启动并验证效果 4.查看版本号: 三.nginx配置文件介绍 1.nginx 文件结构 2.默认的配置 3.nginx的基本配置 四. nginx虚拟主机配置 验证配置文件: 五.nginx全局变量 六.Nginx主要配置 1.静态Http服务器配置 2.反向代理服务器配置 3.负载均衡配置 4.虚拟主机配置 一.Nginx Nginx("engine x")是一款是由俄罗斯的

  • 在Linux(Ubuntu)下搭建PHP环境的操作步骤

    一.安装Apache2 sudo apt-get install apache2 二.测试Apache2 在地址栏输入以下地址,出现如图所示照片,则表明安装成功 http://localhost/ 三.重启apache2服务 sudo /etc/init.d/apache2 restart //'restart'->'stop'关闭服务; 'restart'->'start'启动服务 四.安装PHP sudo apt-get install php5 libapache2-mod-php5 `

  • CentOS 8.1下搭建LEMP(Linux+Nginx+MySQL+PHP)环境(教程详解)

    LEMP是一个软件堆栈,包含一组免费的开源工具,这些工具用于为高流量和动态网站提供动力. LEMP是Linux,Nginx(发音为Engine X),MariaDB/MySQL和PHP的首字母缩写. Nginx是一款开源,强大且高性能的Web服务器,它还可以兼作反向代理. MariaDB是用于存储用户数据的数据库系统,而PHP是用于开发和支持动态网页的服务器端脚本语言. 相关: CentOS 8.1下搭建LAMP(Linux+Apache+MySQL+PHP)环境 https://www.lin

  • Ubuntu下安装并配置VS Code编译C++的方法

    Ubuntu下安装并配置VS Code编译C++ 安装VS Code sudo add-apt-repository ppa:ubuntu-desktop/ubuntu-make sudo apt-get update sudo apt-get install ubuntu-make sudo umake web visual-studio-code 然后按a直接默认同意就可以. 安装插件 打开VS Code后,按crtl + shift + P调出命令行,然后搜索C++,安装微软自己开发的那个

  • Ubuntu下kaldi安装配置图文教程

    因公司业务需要需使用kaldi语音识别工具,现将kaldi环境配置等步骤列出来,用于记录: 1.安装虚拟机: 下载虚拟机VMware,并安装,虚拟机安装比较简单,可自行百度进行安装,在此不再重复. 2.Ubuntu操作系统安装: 下载Ubuntu镜像文件,本人选择的事Ubuntu16.04版本. 3.在虚拟机上安装Ubuntu系统,该步骤会进行详细说明,如下: 3.1 VMware安装完成之后选择创建新虚拟机 3.2 选择下载好的镜像文件 3.3选择下一步,设置系统名称及密码 3.4 继续点击下

  • Linux下从零开始安装配置Nginx服务器+PHP开发环境

    Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,以事件驱动的方式编写,所以有非常好的性能,同时也是一个非常高效的反向代理.负载平衡.其拥有匹配Lighttpd的性能,同时还没有Lighttpd的内存泄漏问题,而且Lighttpd的mod_proxy也有一些问题并且很久没有更新. 因此我打算用其替代Apache应用于Linux服务器上.但是Nginx并不支持cgi方式运行,原因是可以减少因此带来的一些程序上的漏洞.那么我们必须使用FastCGI方式来执行PHP程序. 下面是我成功地配置Ngi

  • Centos7下编译安装配置Nginx+PHP+MySql环境

    序言 这次玩次狠得.除了编译器使用yum安装,其他全部手动编译.哼~ 看似就Nginx.PHP.MySql三个东东,但是它们太尼玛依赖别人了. 没办法,想用它们就得老老实实给它们提供想要的东西. 首先的一些模块依赖一些lib库, 如果你是懒人,就顺着下面的命令分别输入就行了.然后直接看配置篇.(不过这样安装的可不是最新版本的哟) ----------------安装Nginx+PHP+MySql ---------------------- 1.1 安装或更新gcc gcc-c++ 因为我安装的

  • Ubuntu 下安装和配置 FTP服务器

    FTP(文件传输协议)是一个较老且最常用的标准网络协议,用于在两台计算机之间通过网络上传/下载文件.然而, FTP 最初的时候并不安全,因为它仅通过用户凭证(用户名和密码)传输数据,没有进行加密. 警告:如果你打算使用 FTP, 需要考虑通过 SSL/TLS配置 FTP 连接.否则,使用安全 FTP,比如 SFTP 会更好一些. 在这个教程中,我将向你们展示如何在 Ubuntu 中安装.配置并保护 FTP 服务器(VSFTPD 的全称是 "Very Secure FTP Deamon"

  • Gentoo 下安装与配置Nginx+ MySQL + PHP (fastcgi) 环境步骤分享

    一.安装 Nginx 一条命令搞定:USE=fastcgi emerge nginx 新建用户和组:groupadd wwwuseradd www -g www Nginx 安装好后默认会添加 nginx 组和 nginx 用户,不过我本身还是习惯新建个 www 组和 www 用户来做 HTTP 服务用户.若今后 HTTP 服务器更换为 apache 或是 lighttpd 时,用户名和用户组可以不变. 二.安装 MySQL 在装 PHP 前必须先装 MySQL,因为 PHP 里的 MySQL

随机推荐