编辑

简介

Nginx ("engine x") 是一个轻量级,高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名,其特点是占有内存少,并发能力强。

一个nginx.conf例子

这是官网上的一个配置,参照该配置,可以初步一窥nginx设置

user  www www;     # 运行nginx的用户及用户组
worker_processes 2; #启动的进程数
pid /var/run/nginx.pid; #pid文件位置 # [ debug | info | notice | warn | error | crit ]
error_log /var/log/nginx.error_log info; #日志存放及日志等级设置 events {
worker_connections 2000; #每个进程最大的连接数 默认1024
# use [ kqueue | rtsig | epoll | /dev/poll | select | poll ] ;
use kqueue; #使用的处理机制 epoll可以容纳更多请求
} http {
include conf/mime.types; # 加载mime
default_type application/octet-stream; #默认文件类型 log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"'; #设置日志格式 log_format download '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$http_range" "$sent_http_content_range"'; client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m; client_header_buffer_size 1k;
large_client_header_buffers 4 4k; gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain; #设置压缩 output_buffers 1 32k;
postpone_output 1460; sendfile on;
tcp_nopush on; tcp_nodelay on;
send_lowat 12000; keepalive_timeout 75 20; # lingering_time 30;
# lingering_timeout 10;
# reset_timedout_connection on; server { #server段
listen one.example.com;
server_name one.example.com www.one.example.com; access_log /var/log/nginx.access_log main; location / { #location段
proxy_pass http://127.0.0.1/; #设置代理
proxy_redirect off; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m;
client_body_buffer_size 128k; client_body_temp_path /var/nginx/client_body_temp; proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_send_lowat 12000; proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k; proxy_temp_path /var/nginx/proxy_temp; charset koi8-r;
} error_page 404 /404.html; #定义404页面 location /404.html {
root /spool/www; charset on;
source_charset koi8-r;
} location /old_stuff/ {
rewrite ^/old_stuff/(.*)$ /new_stuff/$1 permanent; #rewrite重定向
} location /download/ {
valid_referers none blocked server_names *.example.com; if ($invalid_referer) { #if判断条件
#rewrite ^/ http://www.example.com/;
return 403;
} # rewrite_log on;
# rewrite /download/*/mp3/*.any_ext to /download/*/mp3/*.mp3
rewrite ^/(download/.*)/mp3/(.*)\..*$ /$1/mp3/$2.mp3 break; root /spool/www;
# autoindex on;
access_log /var/log/nginx-download.access_log download;
} location ~* ^.+\.(jpg|jpeg|gif)$ { #为静态资源设置缓存
root /spool/www;
access_log off;
expires 30d;
}
}
}

负载均衡

http {
upstream myproject {
server 127.0.0.1:8000 weight=3;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
server 127.0.0.1:8003;
} server {
listen 80;
server_name www.domain.com;
location / {
proxy_pass http://myproject;
}
}
}

反向代理及缓存

http {
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=STATIC:10m
inactive=24h max_size=1g;
server {
location / {
proxy_pass http://1.2.3.4;
proxy_set_header Host $host;
proxy_cache STATIC;
proxy_cache_valid 200 1d;
proxy_cache_use_stale error timeout invalid_header updating
http_500 http_502 http_503 http_504;
}
}
}

重定向

http {

    server {
listen 80;
server_name www.domain.com;
return 301 https://www.domain.com$request_uri;
}
}

反向代理某G

    server {
listen 443 ssl http2;
server_name google.domain.com; root /usr/share/nginx/html;
index index.html index.htm; ssl on;
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem; ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/letsencrypt/dhparams.pem; location / {
proxy_pass https://www.replace.com/;
}
}

http/2支持

http/2 至少需nginx 1.9版本以上, 编译时openssl版本建议也使用比较高版本不低于 1.0.2

补充链接: https://www.zybuluo.com/phper/note/89391

将可能用到的第三方http请求进行反响代理

    	location ~ "^/proxy/(.*)$" {
resolver 8.8.8.8;
proxy_pass http://$1;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
expires 7d;
}

add a fallback to my proxy in nginx

https://serverfault.com/questions/765483/how-to-add-a-fallback-to-my-proxy-in-nginx

server {
listen 8080;
server_name mydomain;
access_log /log/path/logging.log;
error_page 400 401 402 403 404 405 500 501 502 503 504 @error_page; location @error_page {
root /var/www/html/;
rewrite ^ https://domain.com/error/index.html;
break;
} location / {
proxy_redirect off;
proxy_pass_header Server;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Scheme $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_connect_timeout 5;
proxy_read_timeout 240;
proxy_intercept_errors on; proxy_pass http://127.0.0.1:1337;
}
}

This will redirect all traffic from maindomain:8080 to https://domain.com/error/index.html if the service on http://127.0.0.1:1337 is unavailable(all errors).

Nginx初步配置的更多相关文章

  1. 如何在Nginx下配置PHP程序环境

    1.nginx与PHP的关系 首先来看nginx与php的关系, FastCGI的关系(而不是像apache那样安装成nginx的模块) FastCGI的意思是, 快速的通用网关接口:CGI Comm ...

  2. 从零开始学 Java - CentOS 下 Nginx + Tomcat 配置负载均衡

    为什么现在有非常多的聪明人都在致力于互联网? 最近在读埃隆·马斯克传记,他说「我认为现在有非常多的聪明人都在致力于互联网」. 仔细一想,好像真的是这样的. 我问了自己一个问题:如果你不敲代码了,你能做 ...

  3. Nginx主配置参数详解,Nginx配置网站

    1.Niginx主配置文件参数详解 a.上面博客说了在Linux中安装nginx.博文地址为:http://www.cnblogs.com/hanyinglong/p/5102141.html b.当 ...

  4. nginx + tomcat配置负载均衡

    目标:Nginx做为HttpServer,连接多个tomcat应用实例,进行负载均衡. 注:本例程以一台机器为例子,即同一台机器上装一个nginx和2个Tomcat且安装了JDK1.7. 1.安装Ng ...

  5. spring4+websocket+nginx详细配置

    实现的版本jdk1.7.0_25, tomcat7.0.47.0, Tengine/2.1.1 (nginx/1.6.2), servlet3.0, spring4.2.2 使用maven导入版本3. ...

  6. Nginx Location配置总结

    Nginx Location配置总结 语法规则: location [=|~|~*|^~] /uri/ { - }= 开头表示精确匹配^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即 ...

  7. 理解nginx的配置

    Nginx配置文件主要分成四部分:main(全局设置).server(主机设置).upstream(上游服务器设置,主要为反向代理.负载均衡相关配置)和 location(URL匹配特定位置后的设置) ...

  8. nginx缓存配置的操作记录梳理

    web缓存位于内容源Web服务器和客户端之间,当用户访问一个URL时,Web缓存服务器会去后端Web源服务器取回要输出的内容,然后,当下一个请求到来时,如果访问的是相同的URL,Web缓存服务器直接输 ...

  9. 在nginx中配置如何防止直接用ip访问服务器web server及server_name特性讲解

    看了很多nginx的配置,好像都忽略了ip直接访问web的问题,不利于SEO优化,所以我们希望可以避免直接用IP访问网站,而是域名访问,具体怎么做呢,看下面. 官方文档中提供的方法: If you d ...

随机推荐

  1. 【POJ 2400】 Supervisor, Supervisee(KM求最小权匹配)

    [POJ 2400] Supervisor, Supervisee(KM求最小权匹配) Supervisor, Supervisee Time Limit: 1000MS   Memory Limit ...

  2. Debug与Trace工具类的应用

    在写Console程序的时候,能够使用Console.WriteLine()来时时的输出程序的执行状态和各种參数此刻的信息.可是假设是Windows Form程序,我们要怎样实时的观測程序的执行状况呢 ...

  3. which 命令

    我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索:        which  查看可执行文件的位置.       whereis 查看文件的位置.         ...

  4. POJ 1654 area 解题

    Description You are going to compute the area of a special kind of polygon. One vertex of the polygo ...

  5. ASP.NET中指定自定义HTTP响应标头

    新建一个类HideServerHeaderHelper,继承 IHttpModule,然后重写 OnPreSendRequestHeaders,Dispose,Init方法,如下代码所示 using ...

  6. visual studio code (vscode)像 sublime text 的 ctrl+d 一样多光标选中

    快捷键是 ctrl+m ,返回上一个选中时ctrl+u. 文件 ==>首选项 ==>键盘快捷键() 里面可以查到,下一个是“将选择添加到下一个查找匹配项”,返回上一个是“cursorund ...

  7. android菜鸟学习笔记23----ContentProvider(三)利用内置ContentProvider监听短信及查看联系人

    要使用一个ContentProvider,必须要知道的是它所能匹配的Uri及其数据存储的表的结构. 首先想办法找到访问短信及联系人数据的ContentProvider能接受的Uri: 到github上 ...

  8. nginx服务器的内核调优

    TCP公有类 net.core.somaxconn = 262144 net.core.netdev_max_backlog = 262144 net.ipv4.ip_local_port_range ...

  9. Bootstrap导航栏头部错位问题

    代码: <section class="header"> <div class="container"> <div class=& ...

  10. CENTOS7 修改网卡名称为eth[012...],格式

    具体操作是修改/etc/default/grub文件 在GRUB_CMDLINE_LINUX一行中添加net.ifnames=0 biosdevname=0 保存文件后然后运行 grub2-mkcon ...