nginx应用

应用场景一:web服务器

server {
	listen 80;
	server_name _;
	location / {
		root /data/wwwroot;
		index index.html index.htm;
	}
}
server {
	listen 443 ssl;
	server_name _;
	ssl_certificate /path/to/certificate.crt;
	ssl_certificate _kry /path/to/certificate-kry.key;
	location / {
		root /data/wwwroot;
		index index.html index.htm;
	}
}

应用场景二:反向代理服务器

server {
	listen 80;
	server_name _;
	location / {
		proxy_pass http://192.168.1.1;
		proxy_set_header Host $host; 
		proxy_set_header X-Real-IP $remote_addr;
	}
}

应用场景三:负载均衡器

upstream webservers {
	ip_hash; #会话保持
	server 192.168.1.1:8080;
	server 192.168.1.2:8080;
}
server {
	listen 80;
	server_name _;
	location / {
		proxy_pass http://webservers;
		proxy_set_header Host $host; 
		proxy_set_header X-Real-IP $remote_addr;

应用场景四:URL重定向

#域名重定向
server {
	listen 80;
	server_name old.test.com;
	location / {
		rewrite ^/(.*)$ http://new.test.com/$1;
	}
}
#路径重定向
server {
	listen 80;
	server_name old.test.com;
	location / {
		rewrite ^/old-path/(.*)$ /new-path/$1;
	}
}

应用场景五:防盗链

server {
	listen 80;
	server_name _;
	location ~* \.(gif|jpg|png)$ {
		valid_referers none blocked *.test.com;
		if ($invalid_referer) {
			return 403;
		}
	}
}

应用场景六:手机端重定向PC端

server {
	listen 80;
	server_name _;
	location / {
		if ($http_usre_agent ~* '(android|iphone|ipad)') {
			rewrite ^/(.*)$ https://pc.test.com/$1;
		}
	}
}

应用场景七:基于请求路径转发不同服务

server {
	listen 80;
	server_name _;
	location / {
		proxy_pass http://192.168.1.1;
		proxy_set_header Host $host; 
		proxy_set_header X-Real-IP $remote_addr;
	location /001 {
		proxy_pass http://192.168.1.2;
		proxy_set_header Host $host; 
		proxy_set_header X-Real-IP $remote_addr;
	location /002 {
		proxy_pass http://192.168.1.3;
		proxy_set_header Host $host; 
		proxy_set_header X-Real-IP $remote_addr;
	}
}

补充:四层转发

nginx.conf中加入如下配置时

# 四层负载不在http模块里面,和http模块同级别
http {
}

stream {
    upstream mysql {
	server 127.0.0.1:3306;
    }
    server {
        listen 61306;
        proxy_connect_timeout  10s;
        proxy_timeout  30s;
        proxy_pass  mysql;
    }

解决方法:

1、yum -y install epel-release

2、yum -y install nginx-all-modules.noarch


3、nginx.conf最顶部加入:load_module /usr/lib64/nginx/modules/ngx_stream_module.so;

4、nginx -t 检查nginx配置文件内容语法是否正确(nginx -V查看版本号);

相关推荐

  1. nginx应用

    2024-03-15 19:58:03       22 阅读
  2. nginx应用场景(附配置)

    2024-03-15 19:58:03       23 阅读
  3. linux系统nginx工具的一些应用

    2024-03-15 19:58:03       36 阅读
  4. Docker应用Nginx安装(二)

    2024-03-15 19:58:03       38 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-15 19:58:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-15 19:58:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-15 19:58:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-15 19:58:03       20 阅读

热门阅读

  1. C++中的C标准库、注释和条件编译

    2024-03-15 19:58:03       18 阅读
  2. Jenkins入门指南:自动化构建与部署的艺术

    2024-03-15 19:58:03       18 阅读
  3. LeetCode算法导航

    2024-03-15 19:58:03       22 阅读
  4. 深入探索Kafka底层原理

    2024-03-15 19:58:03       21 阅读
  5. Redis

    2024-03-15 19:58:03       17 阅读
  6. 【设计模式专题之建造者模式】4. 自行车加工

    2024-03-15 19:58:03       19 阅读
  7. c# MD5加密函数

    2024-03-15 19:58:03       16 阅读