Nginx reverse proxy and configuration optimization

Source: Internet
Author: User

The following configuration contains an example of a more comprehensive reverse proxy for the Nginx configuration:

User www www

Worker_processes 8;

Events {

Use Epoll;

Worker_connections 10240;

}

Pid/opt/work/log/nginx.pid;

Error_log/dev/null error;

HTTP {

Include/opt/nginx/conf/mime.types;

Default_type text/html;

Log_format PV ' [$time _local]| $remote _addr| $cookie _suv| $args | $http _referer| the _user_agent $http ';

Log_format Main ' $request _time $remote _addr $cookie _suv [$time _local] $status ' "$request" $body _bytes_sent "$http _ "Referer" "$http _user_agent" "$http _x_forwarded_for" "$cookie _fr";

Log_format pbmain ' $remote _addr-$remote _user [$time _local] "$request"

' $status $body _bytes_sent ' $http _user_agent ';

Access_log off;

Sendfile on;

Keepalive_timeout 0;

gzip on;

#允许客户端请求的最大单个文件字节数

Client_max_body_size 200m;

Upload_progress proxied 8m;

#DNS服务器ip

Resolver 192.168.1.1;

Proxy_set_header Host $http _host;

Proxy_set_header x-forwarded-for $remote _addr;

Proxy_cache_key "$scheme $host$request_uri";

Proxy_cache_path/opt/work/cache levels=1:2:1 keys_zone=js_cache:100m max_size=1024m inactive=15d;

Proxy_cache_valid any 15d;

Upstream Backend {

Ip_hash;

Server 127.0.0.1:8080 weigth=4 max_fails=2 fail_timeout=30s;

Server 127.0.0.1:8081 weigth=3 max_fails=2 fail_timeout=30s;

}

Upstream Livebackends {

Server 192.168.1.15:9090;

Server 192.168.1.16:9090;

#备机

Server 192.168.1.17:9090 backup;

}

server{

Listen 80;

server_name www.myserver1.com;

Error_log/opt/work/log/nginx_error.log error;

Client_max_body_size 200m;

Proxy_set_header Host $http _host;

Proxy_set_header x-forwarded-for $remote _addr;

Proxy_buffer_size 64k;

Proxy_buffers 64k;

#查看服务状态

Location/server-status {

Stub_status on;

Access_log off;

Allow all;

}

Location ^~/PV {

Access_log/opt/work/log1/nginx_access.log PV;

Proxy_pass Http://backend;

}

Location ^~/cc.do

{

Proxy_pass Http://backend;

}

Location ^~/favicon.ico{

Alias/usr/local/src/web/favicon.ico;

}

Location ^~/pv2 {

Access_log/opt/work/log2/nginx_access.log PV;

Access_log/opt/work/log/main/nginx_access_main.log main;

Error_log/opt/work/log/error/nginx_error.log error;

return 204;

}

location/proxy/

{

#后端返回502, 504, execute timeout error, and automatically forward the request to another server in the upstream load pool

Proxy_next_upstream http_502 http_504 error timeout invalid_header;

Proxy_pass Http://backend;

}

#禁止访问除以外的其他目录

Location/{

Deny all;

}

}

server{

Listen 80;

server_name www.myserver2.com;

root/www;

Location ~ T1. (GIF) $ {

root/www;

expires-1;

Access_log/data/log/access.log Pbmain;

Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;

}

Location ~ T2. (GIF) $ {

root/www;

expires-1;

Access_log/data/log/access.log Pbmain;

Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;

if ($args ~ "ltype=1 (&|$)") {

Proxy_pass Http://liveBackends;

}

}

Location/server-status {

Stub_status on;

Access_log off;

Allow 10.0.0.0/8;

Allow 192.168.0.0/16;

Deny all;

}

Error_page 404/404.html;

Error_page 502 503 504/50x.html;

Location =/50x.html {

root HTML;

}

}

}

After configuration, check the configuration file for syntax errors using the command nginx-t (or nginx-t-C profile path), and you can start the/usr/local/nginx/sbin/nginx-c if there are no errors/usr/local/nginx/conf /nginx.conf, you can use the default path configuration file without-C

Calmly stop kill-quit nginx main process number

Smooth reboot kill-hup Nginx main process number

Configuration file modifications can be loaded without downtime reload. Nginx-s Reload

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.