前言
什么是 nginx ? nginx 主要的用在哪里?
后端的小伙伴可能了解的比较多,前端的同学们表示不服,也要学习一波,感受下它的强大之处~~
定义
nginx :静态资源服务器,一般用做 负载均衡 和 反向代理 。
下载
下载地址:nginx下载地址
根据自己的需求下载相应版本的 nginx zip 文件。
配置
事先准备要配置nginx的 云服务器 ,具体可以购买阿里云服务器,详情移步阿里云。
下面按照 aq-nacos-1 这台服务器为例:
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream;
#client_max_body_size 50m;#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;
gzip on; gzip_min_length 1k; gzip_comp_level 2; gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; gzip_vary on; client_max_body_size 1024m; #允许客户端请求的最大单文件字节数 client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
#gzip on;upstream api-gateway {server 127.0.0.1:13000;server 172.26.224.77:13000;}server {listen 80;server_name api.v2.daruiyun.com;location ^~ /ccb/app/api/ {proxy_pass http://api-gateway/ccb/app/api/;}}server {listen 80;server_name bes.aiqin.com;proxy_buffer_size 512k;proxy_buffers 16 512k;proxy_set_header Host $host:$server_port;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;proxy_read_timeout 3600;proxy_send_timeout 3600;location ^~ /bpm/api/ {proxy_pass http://api-gateway/bpm/api/;}location ^~ /bes/api/ {proxy_pass http://api-gateway/bes/web/;}location ^~ /bpmn/ {expires -1s;add_header Cache-Control no-cache;add_header Cache-Control private;alias /usr/fuwushe/html/bpm/;index index.html index.htm;}location / {expires -1s;add_header Cache-Control no-cache;add_header Cache-Control private;alias /usr/fuwushe/html/bes/;index index.html index.htm;try_files $uri $uri/ /index.html;}}server {listen 80;server_name bpm.dingtalk.aiqin.com;proxy_buffer_size 512k;proxy_buffers 16 512k;proxy_set_header Host $host:$server_port;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;proxy_read_timeout 3600;proxy_send_timeout 3600;location ^~ /bpm/api/ {proxy_pass http://127.0.0.1:13000/bpm/api/;}location ^~ /bes/api/ {proxy_pass http://127.0.0.1:13000/bes/web/;}location ^~ /dingtalk/api/ {proxy_pass http://127.0.0.1:13000/dingtalk/web/;}location / {expires -1s;add_header Cache-Control no-cache;add_header Cache-Control private;alias /usr/fuwushe/html/bpm_flaw/;index index.html index.htm;try_files $uri $uri/ /index.html; #这里必须加这个try_files,不然空白}}server {listen 80;server_name bpm.aiqin.com;
proxy_buffer_size 512k; proxy_buffers 16 512k;
proxy_set_header Host $host:$server_port;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;proxy_read_timeout 3600;proxy_send_timeout 3600;location ^~ /bpm/api/ {proxy_pass http://api-gateway/bpm/api/;}location ^~ /bpm/api/auth/ {proxy_pass http://api-gateway/bpm/api/auth/;}location ^~ /dingtalk/api/ {proxy_pass http://api-gateway/dingtalk/web/;}location / {expires -1s;add_header Cache-Control no-cache;add_header Cache-Control private;alias /usr/fuwushe/html/bpm/;index index.html index.htm;try_files $uri $uri/ /index.html;}}server {listen 80;server_name hms.aiqin.com;location ^~ /api/auth/ {proxy_pass http://api-gateway/dls/web/mall/auth/;}location ^~ /api/dls/ {proxy_pass http://api-gateway/dls/web/mall/;}location ^~ /dl/tls/api/ {proxy_pass http://api-gateway/tlsopen/;}location ^~ /dl/tms/api/ {proxy_pass http://api-gateway/tmsopen/;}location ^~ /dl/material/api/ {proxy_pass http://api-gateway/organizationopen/;}location ^~ /dl/coupon/api/ {proxy_pass http://api-gateway/couponopen/;}location ^~ /dl/biz/api/ {proxy_pass http://api-gateway/bizopen/;}location ^~ /da/api/auth/ {proxy_pass http://api-gateway/da/auth/;}location ^~ /da/api/ {proxy_pass http://api-gateway/da/api/;}location ^~ /da/api/ws/ {proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";proxy_pass http://127.0.0.1:19000/;}location ^~ /sp/api/auth/ {proxy_pass http://api-gateway/sp/web/auth/;}location ^~ /sp/api/ {proxy_pass http://api-gateway/sp/web/;}location ^~ /pp/api/auth/ {proxy_pass http://api-gateway/pp/web/auth/;}location ^~ /pp/api/ {proxy_pass http://api-gateway/pp/web/;}location ^~ /scb/api/auth/ {proxy_pass http://api-gateway/scb/web/auth/;}location ^~ /scb/api/ {proxy_pass http://api-gateway/scb/web/;}location ^~ /wms/api/auth/ {proxy_pass http://api-gateway/wms/api/auth/;}location ^~ /wms/api/ {proxy_pass http://api-gateway/wms/api/;}location ^~ /wms-pda/api/auth/ {proxy_pass http://api-gateway/wms-pda/api/auth/;}location ^~ /wms-pda/api/ {proxy_pass http://api-gateway/wms-pda/api/;}location ^~ /hms/api/auth/ {proxy_pass http://api-gateway/hms/web/auth/;}location ^~ /hms/api/ {proxy_pass http://api-gateway/hms/web/;}location ^~ /ccb/app/api/ {proxy_pass http://api-gateway/ccb/app/api/;}location ^~ /bes/api/auth/ {proxy_pass http://api-gateway/bes/web/auth/;}location ^~ /bes/api/ {proxy_pass http://api-gateway/bes/web/;}location ^~ /sms/api/auth/ {proxy_pass http://api-gateway/sms/web/auth/;}location ^~ /sms/api/ {proxy_pass http://api-gateway/sms/web/;}location ^~ /sg/app/ {proxy_pass http://api-gateway/sg/app/;}location ^~ /sg/app/auth/ {proxy_pass http://api-gateway/sg/app/auth/;}location ^~ /ps/app/auth/ {proxy_pass http://api-gateway/ps/app/auth/;}location ^~ /ps/app/ {proxy_pass http://api-gateway/ps/app/;}location ^~ /mp/api/auth/ {proxy_pass http://api-gateway/mp/web/auth/;}location ^~ /mp/api/ {proxy_pass http://api-gateway/mp/web/;}location / {alias /usr/fuwushe/html/hms/;index index.html index.htm;try_files $uri $uri/ /index.html;}}#server {#listen 80;#server_name mp.aiqin.com;#rewrite ^(.*)$ https://$host$1 permanent;#}server {listen 80;listen 443 ssl;server_name mp.aiqin.com;index index_https.html index.htm;ssl_certificate /usr/soft/nginx/cert/mp.aiqin.com.pem;ssl_certificate_key /usr/soft/nginx/cert/mp.aiqin.com.key;ssl_session_timeout 5m;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL;ssl_prefer_server_ciphers on;location ^~ /api {add_header Access-Control-Allow-Methods *;add_header Access-Control-Max-Age 3600;add_header Access-Control-Allow-Credentials true;add_header Access-Control-Allow-Origin $http_origin;add_header Access-Control-Allow-Headers $http_access_control_request_headers;proxy_buffer_size 512k;proxy_buffers 16 512k;proxy_set_header Host $host:$server_port;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;proxy_read_timeout 3600;proxy_send_timeout 3600;proxy_pass http://api-gateway/mp/web/;}location / {expires -1s;add_header Cache-Control no-cache;add_header Cache-Control private;alias /usr/fuwushe/html/mp/;index index.html index.htm;}}server {listen 80;server_name azg.aiqin.com;#charset koi8-r;#access_log logs/host.access.log main;proxy_buffer_size 512k;proxy_buffers 16 512k;proxy_set_header Host $host:$server_port;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;location / {alias /usr/fuwushe/html/sms/;index index.html index.htm;try_files $uri $uri/ /index.html;}location ^~ /api/auth/ {proxy_pass http://api-gateway/dls/web/mall/auth/;}location ^~ /api/dls/ {proxy_pass http://api-gateway/dls/web/mall/;}location ^~ /dl/tls/api/ {proxy_pass http://api-gateway/tlsopen/;}location ^~ /dl/tms/api/ {proxy_pass http://api-gateway/tmsopen/;}location ^~ /dl/material/api/ {proxy_pass http://api-gateway/organizationopen/;}location ^~ /dl/coupon/api/ {proxy_pass http://api-gateway/couponopen/;}location ^~ /dl/biz/api/ {proxy_pass http://api-gateway/bizopen/;}location ^~ /da/api/auth/ {proxy_pass http://api-gateway/da/auth/;}location ^~ /da/api/ {proxy_pass http://api-gateway/da/api/;}location ^~ /da/api/ws/ {proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";proxy_pass http://127.0.0.1:19000/;}location ^~ /sp/api/auth/ {proxy_pass http://api-gateway/sp/web/auth/;}location ^~ /sp/api/ {proxy_pass http://api-gateway/sp/web/;}location ^~ /pp/api/auth/ {proxy_pass http://api-gateway/pp/web/auth/;}location ^~ /pp/api/ {proxy_pass http://api-gateway/pp/web/;}location ^~ /scb/api/auth/ {proxy_pass http://api-gateway/scb/web/auth/;}location ^~ /scb/api/ {proxy_pass http://api-gateway/scb/web/;}location ^~ /wms/api/auth/ {proxy_pass http://api-gateway/wms/api/auth/;}location ^~ /wms/api/ {proxy_pass http://api-gateway/wms/api/;}location ^~ /wms-pda/api/auth/ {proxy_pass http://api-gateway/wms-pda/api/auth/;}location ^~ /wms-pda/api/ {proxy_pass http://api-gateway/wms-pda/api/;}location ^~ /hms/api/auth/ {proxy_pass http://api-gateway/hms/web/auth/;}location ^~ /hms/api/ {proxy_pass http://api-gateway/hms/web/;}location ^~ /ccb/app/api/ {proxy_pass http://api-gateway/ccb/app/api/;}location ^~ /bes/api/auth/ {proxy_pass http://api-gateway/bes/web/auth/;}location ^~ /bes/api/ {proxy_pass http://api-gateway/bes/web/;}location ^~ /sms/api/auth/ {proxy_pass http://api-gateway/sms/web/auth/;}location ^~ /sms/api/ {proxy_pass http://api-gateway/sms/web/;}location ^~ /sg/app/ {proxy_pass http://api-gateway/sg/app/;}location ^~ /sg/app/auth/ {proxy_pass http://api-gateway/sg/app/auth/;}location ^~ /ps/app/auth/ {proxy_pass http://api-gateway/ps/app/auth/;}location ^~ /ps/app/ {proxy_pass http://api-gateway/ps/app/;}location ^~ /mp/api/auth/ {proxy_pass http://api-gateway/mp/web/auth/;}location ^~ /mp/api/ {proxy_pass http://api-gateway/mp/web/;}#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;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# include fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {# listen 8000;# listen somename:8080;# server_name somename alias another.alias;# location / {# root html;# index index.html index.htm;# }#}# HTTPS server##server {# listen 443 ssl;# server_name localhost;# ssl_certificate cert.pem;# ssl_certificate_key cert.key;# ssl_session_cache shared:SSL:1m;# ssl_session_timeout 5m;# ssl_ciphers HIGH:!aNULL:!MD5;# ssl_prefer_server_ciphers on;# location / {# root html;# index index.html index.htm;# }#}
}
---带端口的配置:```jsonuser root;worker_processes 1;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024;}http {client_max_body_size 10m;include mime.types;default_type application/octet-stream;#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;# 开启gzipgzip on;# 启用gzip压缩的最小文件;小于设置值的文件将不会被压缩gzip_min_length 1k;# gzip 压缩级别 1-10gzip_comp_level 2;# 进行压缩的文件类型。gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;# 是否在http header中添加Vary: Accept-Encoding,建议开启gzip_vary on;add_header Cache-Control private;server {listen 18889;server_name dev.codepush.daruiyun.com;location ^~ / {proxy_pass http://127.0.0.1:3000/;}}server {listen 18889;server_name dev.api.daruiyun.com;#charset koi8-r;#access_log logs/host.access.log main;location / {alias /usr/fuwushe/font/;index index.html index.htm;try_files $uri $uri/ /index.html;}location /da/ {alias /usr/fuwushe/font/da/;index index.html index.htm;try_files $uri $uri/ /index.html;}location /zxlmp/img/ {alias /use/zxlmp/img/;index index.html index.htm;try_files $uri $uri/ /index.html;}location ^~ /api/auth/ {proxy_pass http://127.0.0.1:13000/dls/web/mall/auth/;}location ^~ /api/dls/ {proxy_pass http://127.0.0.1:13000/dls/web/mall/;}location ^~ /dl/tls/api/ {proxy_pass http://127.0.0.1:13000/tlsopen/;}location ^~ /dl/tms/api/ {proxy_pass http://127.0.0.1:13000/tmsopen/;}location ^~ /dl/material/api/ {proxy_pass http://127.0.0.1:13000/organizationopen/;}location ^~ /dl/coupon/api/ {proxy_pass http://127.0.0.1:13000/couponopen/;}location ^~ /dl/biz/api/ {proxy_pass http://127.0.0.1:13000/bizopen/;}location ^~ /da/api/auth/ {proxy_pass http://127.0.0.1:13000/da/auth/;}location ^~ /da/api/ {proxy_pass http://127.0.0.1:13000/da/api/;}location ^~ /da/api/ws/ {proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";proxy_pass http://127.0.0.1:19000/;}location ^~ /sp/api/auth/ {proxy_pass http://127.0.0.1:13000/sp/web/auth/;}location ^~ /sp/api/ {proxy_pass http://127.0.0.1:13000/sp/web/;}location ^~ /pp/api/auth/ {proxy_pass http://127.0.0.1:13000/pp/web/auth/;}location ^~ /pp/api/ {proxy_pass http://127.0.0.1:13000/pp/web/;}location ^~ /scb/api/auth/ {proxy_pass http://127.0.0.1:13000/scb/web/auth/;}location ^~ /scb/api/ {proxy_pass http://127.0.0.1:13000/scb/web/;}location ^~ /wms/api/auth/ {proxy_pass http://127.0.0.1:13000/wms/api/auth/;}location ^~ /wms/api/ {proxy_pass http://127.0.0.1:13000/wms/api/;}location ^~ /hms/api/auth/ {proxy_pass http://127.0.0.1:13000/hms/web/auth/;}location ^~ /hms/api/ {proxy_pass http://127.0.0.1:13000/hms/web/;}location ^~ /ccb/app/api/ {proxy_pass http://127.0.0.1:13000/ccb/app/api/;}location ^~ /bes/api/auth/ {proxy_pass http://127.0.0.1:13000/bes/web/auth/;}location ^~ /bes/api/ {proxy_pass http://127.0.0.1:13000/bes/web/;}location ^~ /zxlmp/api/ {proxy_pass http://127.0.0.1:13000/zxlmp/web/;}location ^~ /fcb/app/api/ {proxy_pass http://127.0.0.1:13000/fcb/app/api/;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}server {listen 80;server_name dev.api.daruiyun.com;location / {root /usr/soft/env/front/flows-app/;try_files $uri $uri/ /dl/material/index.html;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}server {listen 80;server_name test.seller.aiqinyouxuan.com;location / {root /usr/soft/env/front/icp_admin/;try_files $uri $uri/ /icp_admin/index.html;expires -1s;add_header Cache-Control no-cache;}location ^~ /icp/api/auth/ {proxy_pass http://127.0.0.1:13000/icp/web/auth/;}location ^~ /icp/api/ {proxy_pass http://127.0.0.1:13000/icp/web/;}}server {listen 6006;server_name dev.api.daruiyun.com;location / {root /usr/soft/env/front/fm/;try_files $uri $uri/ /fm/index.html;expires -1s;add_header Cache-Control no-cache;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}server {listen 6007;server_name dev.api.daruiyun.com;location / {root /usr/soft/env/front/pos-desktop/;try_files $uri $uri/ /index.html;expires -1s;add_header Cache-Control no-cache;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}server {listen 6009;server_name dev.api.daruiyun.com;location / {root /usr/soft/env/front/little_red_horse_admin/;try_files $uri $uri/ /dl/material/index.html;expires -1s;add_header Cache-Control no-cache;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}server {listen 6010;server_name dev.api.daruiyun.com;location / {root /usr/soft/env/front/aq_shop/;try_files $uri $uri/ /aq_shop/index.html;expires -1s;add_header Cache-Control no-cache;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}server {listen 6011;server_name dev.api.daruiyun.com;location / {root /usr/soft/env/front/sms/;try_files $uri $uri/ /index.html;expires -1s;add_header Cache-Control no-cache;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}server {listen 6010;server_name dev.api.daruiyun.com;location / {root /usr/soft/env/front/aq_shop/;try_files $uri $uri/ /aq_shop/index.html;expires -1s;add_header Cache-Control no-cache;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}server {listen 443 ssl;server_name test.aqsg.daruiyun.com;root html;index index_https.html index.htm;ssl on;ssl_certificate /usr/local/nginx/cert/5827163_test.aqsg.daruiyun.com.pem;ssl_certificate_key /usr/local/nginx/cert/5827163_test.aqsg.daruiyun.com.key;ssl_session_timeout 5m;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL;ssl_prefer_server_ciphers on;location /aqsg {alias /usr/fuwushe/font/aqsg;index index.html index.htm;}location / {proxy_pass http://127.0.0.1:13000/;add_header Access-Control-Allow-Methods *;add_header Access-Control-Max-Age 3600;add_header Access-Control-Allow-Credentials true;add_header Access-Control-Allow-Origin $http_origin;add_header Access-Control-Allow-Headers $http_access_control_request_headers;if ($request_method = OPTIONS){return 200;}}}server {listen 443 ssl;server_name test.mengbeishu.daruiyun.com;root html;index index_https.html index.htm;ssl on;ssl_certificate /usr/local/nginx/cert/2632587_test.mengbeishu.daruiyun.com.pem;ssl_certificate_key /usr/local/nginx/cert/2632587_test.mengbeishu.daruiyun.com.key;ssl_session_timeout 5m;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL;ssl_prefer_server_ciphers on;location /mbs {alias /usr/fuwushe/font/mbs;index index.html index.htm;}location /YNL5De3iYY.txt {alias /usr/fuwushe/font/mbs;index YNL5De3iYY.txt;}location / {proxy_pass http://127.0.0.1:13000/;add_header Access-Control-Allow-Methods *;add_header Access-Control-Max-Age 3600;add_header Access-Control-Allow-Credentials true;add_header Access-Control-Allow-Origin $http_origin;add_header Access-Control-Allow-Headers $http_access_control_request_headers;if ($request_method = OPTIONS){return 200;}}}}
配置完成后重新启动服务器:
- cd /usr/soft/nginx/sbin
- ./nginx -s reload
以上是一些 nginx 的基本配置~


