Nginx

Posted 馳騁衿琾

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx相关的知识,希望对你有一定的参考价值。

1.http服务器

2.虚拟主机

3.反向代理,负载均衡

 

安装nginx

配置虚拟主机:

/usr/local/nginx/conf/nginx.conf

一个server就是一个虚拟主机

1.通过端口访问

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }
   server {
        listen       81;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }

2.共用80端口,通过域名访问

server {
        listen       80;
        server_name  www.jd.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }
   server {
        listen       80;
        server_name  www.taobao.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }

  

 

反向代理:

启动两个tomcat:

upstream tomcat1 {
	server 192.168.25.148:8080;
    }
    server {
        listen       80;
        server_name  www.sina.com.cn;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://tomcat1;
            index  index.html index.htm;
        }
    }
    upstream tomcat2 {
	server 192.168.25.148:8081;
    }
    server {
        listen       80;
        server_name  www.sohu.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://tomcat2;
            index  index.html index.htm;
        }
    }

  

负载均衡:

再启动一个tomcat,修改端口为8082,

访问www.sohu.com会轮询访问 weight=2的请求次数越多

    upstream tomcat2 {
	server 192.168.25.148:8081;
        server 192.168.25.148:8082 weight=2;
    }
    server {
        listen       80;
        server_name  www.sohu.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://tomcat2;
            index  index.html index.htm;
        }
    }   

  

    server {

        listen       80;

        server_name  localhost;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 
 

Htmlnginx安装目录下的html目录

 

 

 

技术分享图片        location / {

            root   html;

            index  index.html index.htm;

        }

    }

 

以上是关于Nginx的主要内容,如果未能解决你的问题,请参考以下文章

将 nginx rtmp 片段发送到 WebRTC

text 有用的nginx命令和片段

linux学习:Nginx--常见功能配置片段与优化-06

HLS NGINX-RTMP [错误] 1281#0:* 58 hls:强制片段拆分:10.002 秒

Nginx 跨域

Nginx配置文件详细介绍