> 文章列表 > Ngnix配置config

Ngnix配置config

Ngnix配置config

Docker 安装启动Nginx

1.搜索镜像 search
2.下载镜像 pull
3.运行测试

[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.htmltry_files $uri $uri/ /index.html;index  index.html index.htm;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.htmlerror_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}