Nginx配置反向代理
参考:https://www.runoob.com/w3cnote/nginx-setup-intro.html
nginx.conf内容如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
user nginx; worker_rlimit_nofile 655350; worker_processes auto; worker_cpu_affinity auto; pid /var/run/nginx.pid; error_log /var/log/nginx/error.log warn; events { use epoll; worker_connections 655350; } 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" ' '--"$upstream_addr" $upstream_status $upstream_response_time "$upstream_http_content_type" "$ssl_protocol" "$ssl_cipher"'; log_format access '{"@timestamp":"$time_iso8601",' '"remote_IP":"$remote_addr",' '"time_local":"[$time_local]",' '"request":"$request",' '"status_code":$status,' '"size":$body_bytes_sent,' '"referer":"$http_referer",' '"http_host":"$http_host",' '"DeviceIdentifier":"$http_DeviceIdentifier",' '"DeviceType":"$http_DeviceType",' '"LoanUserID":"$http_LoanUserID",' '"reqs_body":"$request_body",' '"ssl_protocol":"$ssl_protocol",' '"ssl_cipher":"$ssl_cipher",' '"user_agent":"$http_user_agent",' '"x_forward_for":"$http_x_forwarded_for",' '"upstream_addr":"$upstream_addr",' '"upstream_statcode":"$upstream_status",' '"request_time":"$request_time",' '"upstream_resptime":"$upstream_response_time",' '"upstream_conttype":"$upstream_http_content_type",' '"http_Content-Type":"$sent_http_content_type",' '"http_Content-Length":"$sent_http_content_length",' '"http_Connection":"$sent_http_connection",' '"http_Cache-Control":"$sent_http_cache_control",' '"http_Expires":"$sent_http_expires",' '"http_Last-Modified":"$sent_http_last_modified",' '"http_Location":"$sent_http_location",' '"http_X-AspNetMvc-Version":"$sent_http_x_aspnetmvc_version",' '"http_X-AspNet-Version":"$sent_http_x_aspnet_version",' '"http_X-Powered-By":"$sent_http_x_powered_by"}'; log_format access_extend '{"@timestamp":"$time_iso8601",' '"remote_IP":"$remote_addr",' '"time_local":"[$time_local]",' '"request":"$request",' '"status_code":$status,' '"size":$body_bytes_sent,' '"referer":"$http_referer",' '"http_host":"$http_host",' '"DeviceIdentifier":"$http_DeviceIdentifier",' '"DeviceType":"$http_DeviceType",' '"LoanUserID":"$http_LoanUserID",' '"reqs_body":"$request_body",' '"ssl_protocol":"$ssl_protocol",' '"ssl_cipher":"$ssl_cipher",' '"user_agent":"$http_user_agent",' '"x_forward_for":"$http_x_forwarded_for",' '"upstream_addr":"$upstream_addr",' '"upstream_statcode":"$upstream_status",' '"upstream_resptime":"$upstream_response_time",' '"upstream_conttype":"$upstream_http_content_type",' '"http_Cookie":"$http_cookie",' '"http_Content-Type":"$sent_http_content_type",' '"http_Content-Length":"$sent_http_content_length",' '"http_Connection":"$sent_http_connection",' '"http_Cache-Control":"$sent_http_cache_control",' '"http_Expires":"$sent_http_expires",' '"http_Last-Modified":"$sent_http_last_modified",' '"http_Location":"$sent_http_location",' '"http_X-AspNetMvc-Version":"$sent_http_x_aspnetmvc_version",' '"http_X-AspNet-Version":"$sent_http_x_aspnet_version",' '"http_X-Powered-By":"$sent_http_x_powered_by"}'; client_body_temp_path /tmp/nginx_client_body_temp; scgi_temp_path /tmp/nginx_scgi_temp; uwsgi_temp_path /tmp/nginx_uwsgi_temp; fastcgi_temp_path /tmp/nginx_fastcgi_temp; proxy_temp_path /tmp/nginx_proxy_temp; sendfile on; tcp_nopush on; server_tokens off; keepalive_timeout 120; tcp_nodelay on; server_names_hash_bucket_size 128; client_header_buffer_size 32k; client_max_body_size 300m; large_client_header_buffers 4 32k; proxy_pass_request_headers on; proxy_intercept_errors on; proxy_ignore_client_abort on; gzip on; gzip_comp_level 9; gzip_min_length 1K; gzip_buffers 16 32K; gzip_proxied any; gzip_http_version 1.1; gzip_types text/plain text/css text/javascript application/x-httpd-php application/x-javascript application/javascript application/xml image/jpeg image/gif image/png; gzip_vary on; include http.d/*.conf; } stream { include tcp.d/*.conf; }
vim /usr/local/nginx/http.d/mail19.conf,内容如下:
upstream mail801 { server 10.10.22.13:801; #后端服务器 server 10.10.22.14:801; #后端服务器 } server { listen 8011; #对外监听端口 #server_name mailn.test19.com; #对外监听域名 server_name 10.10.22.23; #对外监听IP location / { proxy_pass http://mail801; #使用upstream处的定义的后端服务器池 } }
客户端访问 http://10.10.22.23:8011成功
SSL反向代理:
upstream mail80 { server 10.10.22.133:80; } server { listen 443 ssl; server_name mailn.test19.com; #server_name 10.10.22.223; ssl_certificate /usr/local/nginx/cert/cer_mail.pem; ssl_certificate_key /usr/local/nginx/cert/cer_mail_private.key; location / { proxy_pass http://mail80; } } server { listen 80; server_name mailn.test19.com; rewrite ^(.*)$ https://$host:443$1 permanent; }