原文地址:http://www.whohelpme.com/blog/post/detail/35.html
自己动手写博客1
Nginx 基本配置(log_format,rewrite,proxy)
基本环境
阿里云机器:x86_64-redhat-linux-gnu,Nginx,Tomcat
Nginx gzip压缩
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
Nginx 日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$upstream_addr $upstream_response_time $request_time '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
http_x_forwarded_for 取用户访问IP可用这个字段,在request Header里取到,但后面又做了配置,要以remote到可取
开始日志
access_log /opt/logs/whm_access.log main;
这时我用到了Nginx的重定向,把用户访问不带3W(www)的域名全转到带3W的,便于统计,以及申请QQ Auth认证使用
server_name localhost,www.whohelpme.com,whohelpme.com;
access_log /opt/logs/whm_access.log main;
if ( $host = 'whohelpme.com' ){
rewrite ^/(.*)$ http://www.whohelpme.com/$1 permanent;
}
核心配置
Tomcat和的9000端口,这边需要把所有请求转发到9000,
proxy_set_header X-Real-IP $remote_addr;这一句就是把用户的真实IP透传到应用层使用
location ~ .* {
proxy_pass http://127.0.0.1:9000;
proxy_set_header X-Real-IP $remote_addr;
}
下面付上全部配置
http {
include mime.types;
default_type application/octet-stream;
include proxy.conf;
log_format yundns_log '$server_name $remote_addr [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_a
gent"';
#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;
server_tokens off;
sendfile on;
tcp_nopush on;
server_names_hash_bucket_size 256;
client_header_buffer_size 256k;
#large_client_header_buffers 4 32k;
large_client_header_buffers 4 256k;
client_body_buffer_size 256k;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
client_max_body_size 50m;
keepalive_timeout 120;
fastcgi_intercept_errors on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
#fastcgi_buffer_size 64k;
#fastcgi_buffers 8 64k;
#fastcgi_busy_buffers_size 128k;
#fastcgi_temp_file_write_size 128k;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$upstream_addr $upstream_response_time $request_time '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server {
listen 80 default_server;
server_name localhost,www.whohelpme.com,whohelpme.com;
access_log /opt/logs/whm_access.log main;
if ( $host = 'whohelpme.com' ){
rewrite ^/(.*)$ http://www.whohelpme.com/$1 permanent;
}
index index.jsp index.html;
root /home/default;
location ~ .* {
proxy_pass http://127.0.0.1:9000;
proxy_set_header X-Real-IP $remote_addr;
}
#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;
#}
}
}