Ngnix配置config

Docker 安装启动Nginx

1.搜索镜像 search
2.下载镜像 pull
3.运行测试
4. 单个tomcat 处理能力,500~600, nginx 5万
5. 反向代理, 动静分离,高性能

[root@iZ2ze4nuf6nscouxcmwnasZ ~]# docker images
\REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 a70d36bc331a 6 days ago 449MB
nginx latest f6d0b4767a6c 12 days ago 133MB
centos latest 300e315adb2f 6 weeks ago 209MB
hello-world latest bf756fb1ae65 12 months ago 13.3kB
[root@iZ2ze4nuf6nscouxcmwnasZ ~]# docker run -d --name nginx01 -p 3344:80 nginx
60b706cd23312383bcc5aa7e86b747494e5c329467fc95d80c4b9b42a749b688
[root@iZ2ze4nuf6nscouxcmwnasZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
60b706cd2331 nginx “/docker-entrypoint.…” About a minute ago Up About a minute 0.0.0.0:3344->80/tcp nginx01
[root@iZ2ze4nuf6nscouxcmwnasZ ~]# curl localhost:3344
<!DOCTYPE html>

<html>

<head>
<title>Welcome to nginx!</title>

<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>

<body>

<h1>Welcome to nginx!</h1>

<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

配置文件详解

1.nginx的相关目录

  1. /usr/local/nginx目录是nginx安装成功后,默认生成的目录。
  2. nginx的启动文件在/usr/local/nginx/sbin目录下。
  3. nginx的生效的主配置文件是/usr/local/nginx/conf/nginx.conf

2.nginx配置文件的结构

...            #全局块
events {    #events块
   ...
}
http {    #http块
    ...   
    server {    #server块 
        ...      
        location [PATTERN]  {   #location块
            ...
        }
        location [PATTERN]  {     #location块
            ...
        }
    }
    server {    #server块
      ...
    }
    ...     #http全局块
}

3.include子配置文件

在生效的主配置文件中的http{}配置块中,用include包含其他目录的自定义配置文件,并令其生效。
(每次重新配置文件都需要重启nginx服务器或者重新加载配置文件)

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include    /www/nginx/*.conf;  # 在http的配置块中配置
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

其他目录/www/nginx/*.conf的配置文件:

    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

4.配置反向代理和负载均衡()

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    # 配置反向代理服务器的ip列表
    upstream myserverlist {   
      server 127.0.0.1:8888 weight=1;    # 负载均衡配置权重(加权轮询)
      server 192.168.1.1:9999 weight=1;    # # 负载均衡配置权重(加权轮询)
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
             # 请求myserverlist 定义的服务器列表
            proxy_pass  http://myserverlist ; 
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

5.前端静态资源部署(Server静态详情配置)

server {
       listen       80;
       server_name  127.0.0.1;
       location /prod-api/ {
               # proxy_set_header用来重定义发往后端服务器的请求头
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://localhost:8888/;
        }
        location / {
            root   /www/dist;    # 访问127.0.0.1:80地址的时候,定位到/www/dist文件夹下的首页index.html
            try_files $uri $uri/ /index.html;
            index  index.html index.htm;
        }
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值