suchen
HeimBetrieb und InstandhaltungNginxWas sind die gängigen Konfigurationen und Techniken von Nginx?

一个站点配置多个域名

server {
 listen 80;
 server_name ops-coffee.cn b.ops-coffee.cn;
}

server_name 后跟多个域名即可,多个域名之间用空格分隔

一个服务配置多个站点

server {
 listen 80;
 server_name a.ops-coffee.cn;

 location / {
 root /home/project/pa;
 index index.html;
 }
}

server {
 listen 80;
 server_name ops-coffee.cn b.ops-coffee.cn;

 location / {
 root /home/project/pb;
 index index.html;
 }
}

server {
 listen 80;
 server_name c.ops-coffee.cn;

 location / {
 root /home/project/pc;
 index index.html;
 }
}

基于nginx虚拟主机配置实现,nginx有三种类型的虚拟主机

基于ip的虚拟主机: 需要你的服务器上有多个地址,每个站点对应不同的地址,这种方式使用的比较少

基于端口的虚拟主机: 每个站点对应不同的端口,访问的时候使用ip:port的方式访问,可以修改listen的端口来使用

基于域名的虚拟主机: 使用最广的方式,上边例子中就是用了基于域名的虚拟主机,前提条件是你有多个域名分别对应每个站点,server_name填写不同的域名即可

nginx添加账号密码验证

server {
 location / {
 auth_basic "please input user&passwd";
 auth_basic_user_file key/auth.key;
 }
}

有很多服务通过nginx访问,但本身没有提供账号认证的功能,就可以通过nginx提供的authbase账号密码认证来实现,可以用以下脚本来生成账号的密码

# cat pwd.pl 
#!/usr/bin/perl
use strict;

my $pw=$argv[0] ;
print crypt($pw,$pw)."\n";

使用方法:

# perl pwd.pl ops-coffee.cn
opf8bimqcaxww
# echo "admin:opf8bimqcaxww" > key/auth.key

nginx开启列目录

当你想让nginx作为文件下载服务器存在时,需要开启nginx列目录

server {
 location download {
  autoindex on;

  autoindex_exact_size off;
  autoindex_localtime on;
 }
}

autoindex_exact_size: 为on(默认)时显示文件的确切大小,单位是byte;改为off显示文件大概大小,单位kb或mb或gb

autoindex_localtime: 为off(默认)时显示的文件时间为gmt时间;改为on后,显示的文件时间为服务器时间

默认当访问列出的txt等文件时会在浏览器上显示文件的内容,如果你想让浏览器直接下载,加上下边的配置

if ($request_filename ~* ^.*?\.(txt|pdf|jpg|png)$) {
 add_header content-disposition 'attachment';
}

配置默认站点

server {
 listen 80 default;
}

当一个nginx服务上创建了多个虚拟主机时默认会从上到下查找,如果匹配不到虚拟主机则会返回第一个虚拟主机的内容,如果你想指定一个默认站点时,可以将这个站点的虚拟主机放在配置文件中第一个虚拟主机的位置,或者在这个站点的虚拟主机上配置listen default

不允许通过ip访问

server {
 listen  80 default;
 server_name _;

 return  404;
}

可能有一些未备案的域名或者你不希望的域名将服务器地址指向了你的服务器,这时候就会对你的站点造成一定的影响,需要禁止ip或未配置的域名访问,我们利用上边所说的default规则,将默认流量都转到404去

上边这个方法比较粗暴,当然你也可以配置下所有未配置的地址访问时直接301重定向到你的网站去,也能为你的网站带来一定的流量

server {
 rewrite ^/(.*)$ https://ops-coffee.cn/$1 permanent;
}

直接返回验证文件

location = /xdfyle6tna.txt {
 default_type text/plain;
 return 200 'd6296a84657eb275c05c31b10924f6ea';
}

很多时候微信等程序都需要我们放一个txt的文件到项目里以验证项目归属,我们可以直接通过上边这种方式修改nginx即可,无需真正的把文件给放到服务器上

nginx配置upstream反向代理

http {
 ...
 upstream tomcats {
  server 192.168.106.176 weight=1;
  server 192.168.106.177 weight=1;
 }

 server {
  location /ops-coffee/ { 
   proxy_pass http://tomcats;

   proxy_set_header host $host;
   proxy_set_header x-real-ip $remote_addr;
   proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
   proxy_set_header x-forwarded-proto $scheme;
  }
 }

}

稍不注意可能会落入一个proxy_pass加杠不加杠的陷阱,这里详细说下proxy_pass http://tomcats与proxy_pass http://tomcats/的区别:

虽然只是一个/的区别但结果确千差万别。分为以下两种情况:

1.  目标地址中不带uri(proxy_pass http://tomcats)。此时新的目标url中,匹配的uri部分不做修改,原来是什么就是什么。

location /ops-coffee/ {
 proxy_pass http://192.168.106.135:8181;
}

http://domain/ops-coffee/ -->  http://192.168.106.135:8181/ops-coffee/
http://domain/ops-coffee/action/abc -->  http://192.168.106.135:8181/ops-coffee/action/abc

2.  目标地址中带uri(proxy_pass http://tomcats/,/也是uri),此时新的目标url中,匹配的uri部分将会被修改为该参数中的uri。

location /ops-coffee/ {
 proxy_pass http://192.168.106.135:8181/;
}

http://domain/ops-coffee/ -->  http://192.168.106.135:8181
http://domain/ops-coffee/action/abc -->  http://192.168.106.135:8181/action/abc

nginx upstream开启keepalive

upstream tomcat {
 server ops-coffee.cn:8080;
 keepalive 1024;
}

server {
 location / {
  proxy_http_version 1.1;
  proxy_set_header connection "";

  proxy_pass http://tomcat;
 }
}

nginx在项目中大多数情况下会作为反向代理使用,例如nginx后接tomcat,nginx后接php等,这时我们开启nginx和后端服务之间的keepalive能够减少频繁创建tcp连接造成的资源消耗,配置如上

keepalive: 指定每个nginxworker可以保持的最大连接数量为1024,默认不设置,即nginx作为client时keepalive未生效

proxy_http_version 1.1: 开启keepalive要求http协议版本为http 1.1

proxy_set_header connection "": 为了兼容老的协议以及防止http头中有connection close导致的keepalive失效,这里需要及时清掉http头部的connection

404自动跳转到首页

server {
 location / {
  error_page 404 = @ops-coffee;
 }

 location @ops-coffee {
  rewrite .* / permanent;
 }
}

网站出现404页面不是特别友好,我们可以通过上边的配置在出现404之后给自动跳转到首页去

Das obige ist der detaillierte Inhalt vonWas sind die gängigen Konfigurationen und Techniken von Nginx?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme
Dieser Artikel ist reproduziert unter:亿速云. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen
内存飙升!记一次nginx拦截爬虫内存飙升!记一次nginx拦截爬虫Mar 30, 2023 pm 04:35 PM

本篇文章给大家带来了关于nginx的相关知识,其中主要介绍了nginx拦截爬虫相关的,感兴趣的朋友下面一起来看一下吧,希望对大家有帮助。

nginx限流模块源码分析nginx限流模块源码分析May 11, 2023 pm 06:16 PM

高并发系统有三把利器:缓存、降级和限流;限流的目的是通过对并发访问/请求进行限速来保护系统,一旦达到限制速率则可以拒绝服务(定向到错误页)、排队等待(秒杀)、降级(返回兜底数据或默认数据);高并发系统常见的限流有:限制总并发数(数据库连接池)、限制瞬时并发数(如nginx的limit_conn模块,用来限制瞬时并发连接数)、限制时间窗口内的平均速率(nginx的limit_req模块,用来限制每秒的平均速率);另外还可以根据网络连接数、网络流量、cpu或内存负载等来限流。1.限流算法最简单粗暴的

nginx+rsync+inotify怎么配置实现负载均衡nginx+rsync+inotify怎么配置实现负载均衡May 11, 2023 pm 03:37 PM

实验环境前端nginx:ip192.168.6.242,对后端的wordpress网站做反向代理实现复杂均衡后端nginx:ip192.168.6.36,192.168.6.205都部署wordpress,并使用相同的数据库1、在后端的两个wordpress上配置rsync+inotify,两服务器都开启rsync服务,并且通过inotify分别向对方同步数据下面配置192.168.6.205这台服务器vim/etc/rsyncd.confuid=nginxgid=nginxport=873ho

nginx php403错误怎么解决nginx php403错误怎么解决Nov 23, 2022 am 09:59 AM

nginx php403错误的解决办法:1、修改文件权限或开启selinux;2、修改php-fpm.conf,加入需要的文件扩展名;3、修改php.ini内容为“cgi.fix_pathinfo = 0”;4、重启php-fpm即可。

如何解决跨域?常见解决方案浅析如何解决跨域?常见解决方案浅析Apr 25, 2023 pm 07:57 PM

跨域是开发中经常会遇到的一个场景,也是面试中经常会讨论的一个问题。掌握常见的跨域解决方案及其背后的原理,不仅可以提高我们的开发效率,还能在面试中表现的更加

nginx部署react刷新404怎么办nginx部署react刷新404怎么办Jan 03, 2023 pm 01:41 PM

nginx部署react刷新404的解决办法:1、修改Nginx配置为“server {listen 80;server_name https://www.xxx.com;location / {root xxx;index index.html index.htm;...}”;2、刷新路由,按当前路径去nginx加载页面即可。

nginx怎么禁止访问phpnginx怎么禁止访问phpNov 22, 2022 am 09:52 AM

nginx禁止访问php的方法:1、配置nginx,禁止解析指定目录下的指定程序;2、将“location ~^/images/.*\.(php|php5|sh|pl|py)${deny all...}”语句放置在server标签内即可。

Linux系统下如何为Nginx安装多版本PHPLinux系统下如何为Nginx安装多版本PHPMay 11, 2023 pm 07:34 PM

linux版本:64位centos6.4nginx版本:nginx1.8.0php版本:php5.5.28&php5.4.44注意假如php5.5是主版本已经安装在/usr/local/php目录下,那么再安装其他版本的php再指定不同安装目录即可。安装php#wgethttp://cn2.php.net/get/php-5.4.44.tar.gz/from/this/mirror#tarzxvfphp-5.4.44.tar.gz#cdphp-5.4.44#./configure--pr

See all articles

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

Heiße Werkzeuge

DVWA

DVWA

Damn Vulnerable Web App (DVWA) ist eine PHP/MySQL-Webanwendung, die sehr anfällig ist. Seine Hauptziele bestehen darin, Sicherheitsexperten dabei zu helfen, ihre Fähigkeiten und Tools in einem rechtlichen Umfeld zu testen, Webentwicklern dabei zu helfen, den Prozess der Sicherung von Webanwendungen besser zu verstehen, und Lehrern/Schülern dabei zu helfen, in einer Unterrichtsumgebung Webanwendungen zu lehren/lernen Sicherheit. Das Ziel von DVWA besteht darin, einige der häufigsten Web-Schwachstellen über eine einfache und unkomplizierte Benutzeroberfläche mit unterschiedlichen Schwierigkeitsgraden zu üben. Bitte beachten Sie, dass diese Software

PHPStorm Mac-Version

PHPStorm Mac-Version

Das neueste (2018.2.1) professionelle, integrierte PHP-Entwicklungstool

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

MinGW – Minimalistisches GNU für Windows

MinGW – Minimalistisches GNU für Windows

Dieses Projekt wird derzeit auf osdn.net/projects/mingw migriert. Sie können uns dort weiterhin folgen. MinGW: Eine native Windows-Portierung der GNU Compiler Collection (GCC), frei verteilbare Importbibliotheken und Header-Dateien zum Erstellen nativer Windows-Anwendungen, einschließlich Erweiterungen der MSVC-Laufzeit zur Unterstützung der C99-Funktionalität. Die gesamte MinGW-Software kann auf 64-Bit-Windows-Plattformen ausgeführt werden.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Leistungsstarke integrierte PHP-Entwicklungsumgebung