Ubuntu下如何搭建与配置Nginx服务(ubuntu搭建nginx服务器教程)

Ubuntu下如何搭建与配置Nginx服务(ubuntu搭建nginx服务器教程)

浏览次数:
信息来源: 用户投稿
更新日期: 2026-04-27
文章简介

动态服务,nginx——fastcgi的方式运行PHP,jsp。(PHP并发在500-1500,MySQL并发在300-1500)。反向代理,负载均衡。日pv2000W以

2025阿里云双十一服务器活动

  • 动态服务,nginx——fastcgi的方式运行PHP,jsp。(PHP并发在500-1500,MySQL并发在300-1500)。

  • 反向代理,负载均衡。日pv2000W以下,都可以直接用nginx做代理。

  • 缓存服务。类似SQUID,VARNISH。

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

    sudoaptinstallnginx

    2、安装后的位置:

  • /usr/share/nginx:存放静态文件

  • servicenginxstart#启动nginx

    servicenginxreload#重新加载nginx配置文件

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

    nginx-sstop#停止Nginx

    4、查看版本号:

    nginxversion:nginx/1.14.0(Ubuntu)

  • location块:配置请求的路由,以及各种页面的处理情况。

  • ...#全局块。配置影响nginx全局的指令。

    events{#events块。配置影响nginx服务器或与用户的网络连接。

    http#http块。可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。

    server#server块。配置虚拟主机的相关参数,一个http中可以有多个server。

    location[PATTERN]#location块。配置请求的路由,以及各种页面的处理情况。

    #YoushouldlookatthefollowingURL'sinordertograspasolidunderstanding

    #ofNginxconfigurationfilesinordertofullyunleashthepowerofNginx.

    #https://www.nginx.com/resources/wiki/start/

    #https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/

    #https://wiki.debian.org/Nginx/DirectoryStructure

    #Inmostcases,administratorswillremovethisfilefromsites-enabled/and

    #leaveitasreferenceinsideofsites-availablewhereitwillcontinuetobe

    #updatedbythenginxpackagingteam.

    #Thisfilewillautomaticallyloadconfigurationfilesprovidedbyother

    #applications,suchasDrupalorWordpress.Theseapplicationswillbemade

    #availableunderneathapathwiththatpackagename,suchas/drupal8.

    #Pleasesee/usr/share/doc/nginx-doc/examples/formoredetailedexamples.

    #Defaultserverconfiguration

    listen[::]:80default_server;

    #listen443ssldefault_server;

    #listen[::]:443ssldefault_server;

    #Note:YoushoulddisablegzipforSSLtraffic.

    #See:https://bugs.debian.org/773332

    #Readuponssl_cipherstoensureasecureconfiguration.

    #See:https://bugs.debian.org/765782

    #Selfsignedcertsgeneratedbythessl-certpackage

    #Don'tusetheminaproductionserver!

    #includesnippets/snakeoil.conf;

    #Addindex.phptothelistifyouareusingPHP

    indexindex.htmlindex.htmindex.nginx-debian.html;

    #Firstattempttoserverequestasfile,then

    #asdirectory,thenfallbacktodisplayinga404.

    #passPHPscriptstoFastCGIserver

    #includesnippets/fastcgi-php.conf;

    Withphp-fpm(orotherunixsockets):

    #fastcgi_passunix:/var/run/php/php7.0-fpm.sock;

    Withphp-cgi(orothertcpsockets):

    #fastcgi_pass127.0.0.1:9000;

    #denyaccessto.htaccessfiles,ifApache'sdocumentroot

    #VirtualHostconfigurationforexample.com

    #Youcanmovethattoadifferentfileundersites-available/andsymlinkthat

    #tosites-enabled/toenableit.

    #root/var/www/example.com;

    Ubuntu下如何搭建与配置Nginx服务,ubuntu搭建nginx服务器教程

    配置用户或者组,默认为nobodynobody。

    允许生成的进程数,默认为1

    指定nginx进程运行文件存放地址

    error_loglog/error.logdebug;#制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg

    accept_mutexon;#设置网路连接序列化,防止惊群现象发生,默认为on

    multi_accepton;#设置一个进程是否同时接受多个网络连接,默认为off

    事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport

    worker_connections1024;#最大连接数,默认为512

    includemime.types;#文件扩展名与文件类型映射表

    default_typeapplication/octet-stream;#默认文件类型,默认为text/plain

    log_formatmyFormat'$remote_addr–$remote_user[$time_local]$request$status$body_bytes_sent$http_referer$http_user_agent$http_x_forwarded_for';#自定义格式

    access_loglog/access.logmyFormat;#combined为日志格式的默认值

    sendfileon;#允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。

    sendfile_max_chunk100k;#每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。

    keepalive_timeout65;#连接超时时间,默认为75s,可以在http,server,location块。

    server192.168.10.121:3333backup;#热备

    error_page404https://www.baidu.com;#错误页

    keepalive_requests120;#单连接请求上限次数。

    server_name127.0.0.1;#监听地址

    location~*^.+${#请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。

    proxy_passhttp://mysvr;#请求转向mysvr定义的服务器列表

    allow172.18.5.54;#允许的ip

    }

    四、nginx虚拟主机配置

    #下面是server虚拟主机的配置段

    server_namelocalhost;#域名

    indexindex.htmlindex.htmindex.php;

    root/usr/local/webserver/nginx/html;#站点目录

    location~.*\.(php|php5)?$

    #fastcgi_passunix:/tmp/php-cgi.sock;

    fastcgi_pass127.0.0.1:9000;

    location~.*\.(gif|jpg|jpeg|png|bmp|swf|ico)$

    nginx:theconfigurationfile/etc/nginx/nginx.confsyntaxisok

    nginx:configurationfile/etc/nginx/nginx.conftestissuccessful

  • 访问链接是:http://localhost:88/test1/test.php

    网站路径是:/var/www/html

    $request_uri:<ahref="http://localhost:88/test1/test.php"rel="externalnofollow"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

    server_namelocalhost192.168.1.100;  #域名

    location/{             #代表这是项目根目录

    root/usr/share/nginx/www; #虚拟目录

    proxy_passhttp://192.168.0.112:8080;  #应用服务器HTTP地址

    }

    3、负载均衡配置

    当网站访问量非常大,也摊上事儿了。因为网站越来越慢,一台服务器已经不够用了。因此,可以将同一应用部署在多台服务器上,以将众多用户请求分配至多台机器进行处理。即使其中一台服务器故障,只要其他服务器正常运行,用户仍然可以正常使用,这是多台服务器带来的好处。Nginx可以通过反向代理来实现负载均衡。

    server192.168.0.111:8080;  #应用服务器1

    server192.168.0.112:8080;  #应用服务器2

    }

    4、虚拟主机配置

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

    return444;  #过滤其他域名的请求,返回444状态码

    server_namewww.aaa.com;  #www.aaa.com域名

    proxy_passhttp://localhost:8080;  #对应端口号8080

    server_namewww.bbb.com;  #www.bbb.com域名

    proxy_passhttp://localhost:8081;  #对应端口号8081

    }

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

    以上就是Ubuntu下如何搭建与配置Nginx服务的详细内容,更多请关注主机测评网其它相关文章!

    标签:
    数据中心与服务器机房的优缺点(数据中心服务器知识全解)
    « 上一篇
    返回列表
    下一篇 »

    如本文对您有帮助,就请抽根烟吧!