nginx 安装、配置、负载均衡、反向代理<二>
来源:互联网 发布:yum 安装pure ftpd 编辑:程序博客网 时间:2024/06/06 01:35
nginx 配置
本文主讲nginx的配置
nginx的安装,详见:nginx 安装、配置、负载均衡、反向代理<一>
准备工作: 一台能联公网的CentOS 7.x服务器
# root 登录(因为要使用80端口,一般情况下需要root权限) cd /etc/nginx # 备份一下原来的nginx.conf文件__很重要!!!__ cp nginx.conf nginx.conf.cp # 检查一下nginx安装正确 systemctl status mginx # 编辑nginx.conf配置文件 # 在此重申一遍,一定要做好备份工作,不然自己配置的有问题只能找别人要一份备份文件(文末有)或者重装了 vi nginx.conf
nginx.conf配置文件可配置项说明
负载均衡配置
upstream <命名,eg:backend_test> { #ip_hash; # 当负载两台以上用ip来hash解决session的问题 server 192.168.10.100:8080 max_fails=2 fail_timeout=30s ; server 192.168.10.101:8080 max_fails=2 fail_timeout=30s ; } # server段配合负载均衡的设置 server { listen 80; server_name www.aliyuntest.com; #要访问的域名,这里用的测试域名,如果有多个,用逗号分开 charset utf8; location / { proxy_pass http://backend_test; #这里backend_test是上面的负载的名称,映射到代理服务器 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
- 以上,配置完负载均衡,在浏览器访问
www.aliyuntest.com
会被均衡到我们配置的两个IP上的服务上
反向代理配置
server { listen 80; server_name app.aliyuntest.com; #反向代理到另一个域名(可以使同一个域名下的多个二级域名) charset utf8; #charset koi8-r; #access_log logs/host.access.log main; location / { # 这里的 / 表示的是访问的 app.aliyuntest.com/ proxy_pass http://110.110.110.110:8000; #这里可以填本机开启的服务,也可以填其他机器开启的服务,格式http://Ip:port或者http://域名:port (端口为80的不用写,为443的也不写,但要把http改为https) proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
- 以上,负载均衡配置的server部分,其实也是一个反向代理的过程
- 反向代理中比较重要的就是
location
字段的配置,一般情况下,这些配置足够了,更多详细的配置请百度
gzip 压缩
gzip on; # 开启gzip压缩 gzip_min_length 1k; # 压缩的临界值,大于1K的才压缩 gzip_buffers 4 512k; # gzip压缩的内存大小 #gzip_http_version 1.0; # 用了nginx反向代理的默认是http/1.1,需要http:/1.0的取消注释 gzip_comp_level 6; # 压缩级别,越大压缩的越小,服务器压力越大 gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/javascript; # 压缩文件格式,浏览器-控制台-network-http请求的Respose-Headers中查看content-type字段 gzip_vary off; gzip_disable "MSIE [1-6]\."; # IE6不使用gzip压缩
# 检测gzip是否开启成功 curl -I -H "Accept-Encoding: gzip, deflate" "http://域名/路由"
初始状态的 nginx.conf 配置文件内容可免费复制使用-,-
#user nobody; worker_processes 1; #error_log /var/log/nginx/error.log; #error_log /var/log/nginx/error.log notice; #error_log /var/log/nginx/error.log info; #pid /var/run/nginx/nginx.pid; events { worker_connections 1024; } http { 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 /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root 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; } # 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; # } #} }
配置了反向代理的 nginx.conf 配置文件内容仅供参考
#user nobody; worker_processes 4; #error_log /var/log/nginx/error.log; #error_log /var/log/nginx/error.log notice; #error_log /var/log/nginx/error.log info; #pid /var/run/nginx/nginx.pid; events { worker_connections 1024; } http { 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 /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; gzip on; gzip_min_length 1k; gzip_buffers 4 512k; #gzip_http_version 1.0; gzip_comp_level 6; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/javascript; gzip_vary off; gzip_disable "MSIE [1-6]\."; server { listen 80; server_name app.aliyun_test.com; charset utf8; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { proxy_pass http://110.110.110.110:8000; #这里proxy_test是上面的负载的名称,映射到代理服务器,可以是ip加端口, 或url proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } server { listen 80; server_name www.aliyun_test.top; charset utf8; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { proxy_pass http://www.my_aliyun_test.cn; #这里proxy_test是上面的负载的名称,映射到代理服务器,可以是ip加端口, 或url proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } #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; # } #} }
以上,初步使用nginx,欢迎大神拍砖指导,如有不明白的,欢迎随时交流
阅读全文
0 0
- nginx 安装、配置、负载均衡、反向代理<二>
- Centos7安装Nginx及配置反向代理和负载均衡
- nginx 安装、配置、负载均衡、反向代理<一>
- nginx反向代理配置,负载均衡
- nginx反向代理和负载均衡配置
- nginx 反向代理 负载均衡配置
- nginx反向代理、负载均衡配置
- nginx 反向代理和负载均衡配置
- Nginx反向代理以及负载均衡配置
- nginx配置实现反向代理&负载均衡
- Nginx 反向代理 负载均衡 虚拟主机配置
- 反向代理负载均衡Nginx
- nginx反向代理,负载均衡
- 负载均衡 反向代理 nginx
- nginx 反向代理-负载均衡
- Nginx 反向代理 + 负载均衡
- nginx反向代理负载均衡
- Nginx的介绍,安装和配置,Nginx的反向代理,负载均衡
- Java DES 加密
- Android Studio APK分析工具--APK Analyzer
- Spring MVC防止数据重复提交(防止二次提交)
- 第十一周 【项目1
- Android,RecyclerView,divider,分割线
- nginx 安装、配置、负载均衡、反向代理<二>
- gradle打包出现Could reserve enough space for 2097152KB for object heap
- mysql与oracle的日期/时间函数小结
- Compiler tools not found
- 暗黑字符串问题(java)
- vue2.0监听数组中对象属性的变化
- C++泛型算法简要整理
- springboot(七):springboot+mybatis多数据源最简解决方案
- SpringBoot中使用Springsecurity实现权限控制