手把手以实例叫你学习nginx配置
Author:[email protected] Date:
所谓手把手,就是把自己的nginx.conf配置文件copy出来,然后在仔细看代码!
做了注释,最底下是最近的配置文件
#user nobody;
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 {
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;
gzip on;
#一个监听服务
server {
# 比如监听 80端口下服务
listen 80;
# 监听地址
server_name localhost;
# NGINX的proxy_redirect功能比较强大,其作用是对发送给客户端的URL进行修改。以例子说明:
# server {
# listen 80;
# server_name localhost;
# location {
# proxy_pass http://10.10.10.1:9080;
# }
# }
# 这段配置一般情况下都正常,但偶尔会出错, 错误在什么地方呢? 抓包发现服务器给客户端的跳转指令里加了端口号,如 Location: http://test.abc.com:9080/abc.html 。因为nginx服务器侦听的是80端口,所以这样的URL给了客户端,必然会出错.针对这种情况, 加一条proxy_redirect指令: proxy_redirect http://test.abc.com:9080/ / ,把所有“http://test.abc.com:9080/”的内容替换成“/”再发给客户端,就解决了。
# server {
# listen 80;
# server_name test.abc.com;
# proxy_redirect http://test.abc.com:9080/ /;
# location / {
# proxy_pass http://10.10.10.1:9080;
# }
# }
#
proxy_redirect http://192.168.1.85:8080/jsse/ /;
# 例子,有如下匹配规则:
# location = / {
# #规则A
# }
# location = /login {
# #规则B
# }
# location ^~ /static/ {
# #规则C
# }
# location ~ \.(gif|jpg|png|js|css)$ {
# #规则D
# }
# location ~* \.png$ {
# #规则E
# }
# location !~ \.xhtml$ {
# #规则F
# }
# location !~* \.xhtml$ {
# #规则G
# }
# location / {
# #规则H
# }
# 那么产生的效果如下:
# 访问根目录/, 比如http://localhost/ 将匹配规则A
# 访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H
# 访问 http://localhost/static/a.html 将匹配规则C
# 访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用,而 http://localhost/static/c.png 则优先匹配到规则C
# 访问 http://localhost/a.PNG 则匹配规则E,而不会匹配规则D,因为规则E不区分大小写。
# 访问 http://localhost/a.xhtml 不会匹配规则F和规则G,http://localhost/a.XHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。
# 访问 http://localhost/category/id/1111 则最终匹配到规则H,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。
# 防盗链
# location ~* \.(gif|jpg|swf)$ {
# valid_referers none blocked start.igrow.cn sta.igrow.cn;
# if ($invalid_referer) {
# rewrite ^/ http://$host/logo.png;
# }
# }
# 六、根据文件类型设置过期时间
# location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
# if (-f $request_filename) {
# expires 1h;
# break;
# }
# }
# 七、禁止访问某个目录
# location ~* \.(txt|doc)${
# root /data/www/wwwroot/linuxtone/test;
# deny all;
# }
location / {
# 转发地址
proxy_pass http://localhost:8484;
proxy_redirect default;
proxy_set_header Host $host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /jsse/ {
proxy_pass http://192.168.1.85:8080/jsse/;
proxy_redirect default;
proxy_set_header Host $host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 8038;
server_name localhost;
#charset koi8-r;
#access_log logs/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;
# }
#}
}
#user nobody;
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 {
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;
gzip on;
#一个监听服务
server {
# 比如监听 80端口下服务
listen 80;
# 监听地址
server_name localhost;
# NGINX的proxy_redirect功能比较强大,其作用是对发送给客户端的URL进行修改。以例子说明:
# server {
# listen 80;
# server_name localhost;
# location {
# proxy_pass http://10.10.10.1:9080;
# }
# }
# 这段配置一般情况下都正常,但偶尔会出错, 错误在什么地方呢? 抓包发现服务器给客户端的跳转指令里加了端口号,如 Location: http://test.abc.com:9080/abc.html 。因为nginx服务器侦听的是80端口,所以这样的URL给了客户端,必然会出错.针对这种情况, 加一条proxy_redirect指令: proxy_redirect http://test.abc.com:9080/ / ,把所有“http://test.abc.com:9080/”的内容替换成“/”再发给客户端,就解决了。
# server {
# listen 80;
# server_name test.abc.com;
# proxy_redirect http://test.abc.com:9080/ /;
# location / {
# proxy_pass http://10.10.10.1:9080;
# }
# }
#
proxy_redirect http://192.168.1.85:8080/jsse/ /;
# 例子,有如下匹配规则:
# location = / {
# #规则A
# }
# location = /login {
# #规则B
# }
# location ^~ /static/ {
# #规则C
# }
# location ~ \.(gif|jpg|png|js|css)$ {
# #规则D
# }
# location ~* \.png$ {
# #规则E
# }
# location !~ \.xhtml$ {
# #规则F
# }
# location !~* \.xhtml$ {
# #规则G
# }
# location / {
# #规则H
# }
# 那么产生的效果如下:
# 访问根目录/, 比如http://localhost/ 将匹配规则A
# 访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H
# 访问 http://localhost/static/a.html 将匹配规则C
# 访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用,而 http://localhost/static/c.png 则优先匹配到规则C
# 访问 http://localhost/a.PNG 则匹配规则E,而不会匹配规则D,因为规则E不区分大小写。
# 访问 http://localhost/a.xhtml 不会匹配规则F和规则G,http://localhost/a.XHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。
# 访问 http://localhost/category/id/1111 则最终匹配到规则H,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。
# 防盗链
# location ~* \.(gif|jpg|swf)$ {
# valid_referers none blocked start.igrow.cn sta.igrow.cn;
# if ($invalid_referer) {
# rewrite ^/ http://$host/logo.png;
# }
# }
# 六、根据文件类型设置过期时间
# location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
# if (-f $request_filename) {
# expires 1h;
# break;
# }
# }
# 七、禁止访问某个目录
# location ~* \.(txt|doc)${
# root /data/www/wwwroot/linuxtone/test;
# deny all;
# }
location / {
# 转发地址
proxy_pass http://localhost:8484;
proxy_redirect default;
proxy_set_header Host $host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /jsse/ {
proxy_pass http://192.168.1.85:8080/jsse/;
proxy_redirect default;
proxy_set_header Host $host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 8038;
server_name localhost;
#charset koi8-r;
#access_log logs/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;
# }
#}
}
#user nobody;
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 {
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;
gzip on;
#一个监听服务
server {
# 比如监听 80端口下服务
listen 80;
# 监听地址
server_name localhost;
# NGINX的proxy_redirect功能比较强大,其作用是对发送给客户端的URL进行修改。以例子说明:
# server {
# listen 80;
# server_name localhost;
# location {
# proxy_pass http://10.10.10.1:9080;
# }
# }
# 这段配置一般情况下都正常,但偶尔会出错, 错误在什么地方呢? 抓包发现服务器给客户端的跳转指令里加了端口号,如 Location: http://test.abc.com:9080/abc.html 。因为nginx服务器侦听的是80端口,所以这样的URL给了客户端,必然会出错.针对这种情况, 加一条proxy_redirect指令: proxy_redirect http://test.abc.com:9080/ / ,把所有“http://test.abc.com:9080/”的内容替换成“/”再发给客户端,就解决了。
# server {
# listen 80;
# server_name test.abc.com;
# proxy_redirect http://test.abc.com:9080/ /;
# location / {
# proxy_pass http://10.10.10.1:9080;
# }
# }
#
proxy_redirect http://192.168.1.85:8080/jsse/ /;
# 例子,有如下匹配规则:
# location = / {
# #规则A
# }
# location = /login {
# #规则B
# }
# location ^~ /static/ {
# #规则C
# }
# location ~ \.(gif|jpg|png|js|css)$ {
# #规则D
# }
# location ~* \.png$ {
# #规则E
# }
# location !~ \.xhtml$ {
# #规则F
# }
# location !~* \.xhtml$ {
# #规则G
# }
# location / {
# #规则H
# }
# 那么产生的效果如下:
# 访问根目录/, 比如http://localhost/ 将匹配规则A
# 访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H
# 访问 http://localhost/static/a.html 将匹配规则C
# 访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用,而 http://localhost/static/c.png 则优先匹配到规则C
# 访问 http://localhost/a.PNG 则匹配规则E,而不会匹配规则D,因为规则E不区分大小写。
# 访问 http://localhost/a.xhtml 不会匹配规则F和规则G,http://localhost/a.XHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。
# 访问 http://localhost/category/id/1111 则最终匹配到规则H,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。
# 防盗链
# location ~* \.(gif|jpg|swf)$ {
# valid_referers none blocked start.igrow.cn sta.igrow.cn;
# if ($invalid_referer) {
# rewrite ^/ http://$host/logo.png;
# }
# }
# 六、根据文件类型设置过期时间
# location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
# if (-f $request_filename) {
# expires 1h;
# break;
# }
# }
# 七、禁止访问某个目录
# location ~* \.(txt|doc)${
# root /data/www/wwwroot/linuxtone/test;
# deny all;
# }
location / {
# 转发地址
proxy_pass http://localhost:8484;
proxy_redirect default;
proxy_set_header Host $host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /jsse/ {
proxy_pass http://192.168.1.85:8080/jsse/;
proxy_redirect default;
proxy_set_header Host $host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 8038;
server_name localhost;
#charset koi8-r;
#access_log logs/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;
# }
#}
}
#user nobody;
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 {
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;
gzip on;
#一个监听服务
server {
# 比如监听 80端口下服务
listen 80;
# 监听地址
server_name localhost;
# NGINX的proxy_redirect功能比较强大,其作用是对发送给客户端的URL进行修改。以例子说明:
# server {
# listen 80;
# server_name localhost;
# location {
# proxy_pass http://10.10.10.1:9080;
# }
# }
# 这段配置一般情况下都正常,但偶尔会出错, 错误在什么地方呢? 抓包发现服务器给客户端的跳转指令里加了端口号,如 Location: http://test.abc.com:9080/abc.html 。因为nginx服务器侦听的是80端口,所以这样的URL给了客户端,必然会出错.针对这种情况, 加一条proxy_redirect指令: proxy_redirect http://test.abc.com:9080/ / ,把所有“http://test.abc.com:9080/”的内容替换成“/”再发给客户端,就解决了。
# server {
# listen 80;
# server_name test.abc.com;
# proxy_redirect http://test.abc.com:9080/ /;
# location / {
# proxy_pass http://10.10.10.1:9080;
# }
# }
#
proxy_redirect http://192.168.1.85:8080/jsse/ /;
# 例子,有如下匹配规则:
# location = / {
# #规则A
# }
# location = /login {
# #规则B
# }
# location ^~ /static/ {
# #规则C
# }
# location ~ \.(gif|jpg|png|js|css)$ {
# #规则D
# }
# location ~* \.png$ {
# #规则E
# }
# location !~ \.xhtml$ {
# #规则F
# }
# location !~* \.xhtml$ {
# #规则G
# }
# location / {
# #规则H
# }
# 那么产生的效果如下:
# 访问根目录/, 比如http://localhost/ 将匹配规则A
# 访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H
# 访问 http://localhost/static/a.html 将匹配规则C
# 访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用,而 http://localhost/static/c.png 则优先匹配到规则C
# 访问 http://localhost/a.PNG 则匹配规则E,而不会匹配规则D,因为规则E不区分大小写。
# 访问 http://localhost/a.xhtml 不会匹配规则F和规则G,http://localhost/a.XHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。
# 访问 http://localhost/category/id/1111 则最终匹配到规则H,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。
# 防盗链
# location ~* \.(gif|jpg|swf)$ {
# valid_referers none blocked start.igrow.cn sta.igrow.cn;
# if ($invalid_referer) {
# rewrite ^/ http://$host/logo.png;
# }
# }
# 六、根据文件类型设置过期时间
# location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
# if (-f $request_filename) {
# expires 1h;
# break;
# }
# }
# 七、禁止访问某个目录
# location ~* \.(txt|doc)${
# root /data/www/wwwroot/linuxtone/test;
# deny all;
# }
location / {
# 转发地址
proxy_pass http://localhost:8484;
proxy_redirect default;
proxy_set_header Host $host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /jsse/ {
proxy_pass http://192.168.1.85:8080/jsse/;
proxy_redirect default;
proxy_set_header Host $host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 8038;
server_name localhost;
#charset koi8-r;
#access_log logs/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;
# }
#}
}
#user nobody;
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 {
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;
gzip on;
#一个监听服务
server {
# 比如监听 80端口下服务
listen 80;
# 监听地址
server_name localhost;
# NGINX的proxy_redirect功能比较强大,其作用是对发送给客户端的URL进行修改。以例子说明:
# server {
# listen 80;
# server_name localhost;
# location {
# proxy_pass http://10.10.10.1:9080;
# }
# }
# 这段配置一般情况下都正常,但偶尔会出错, 错误在什么地方呢? 抓包发现服务器给客户端的跳转指令里加了端口号,如 Location: http://test.abc.com:9080/abc.html 。因为nginx服务器侦听的是80端口,所以这样的URL给了客户端,必然会出错.针对这种情况, 加一条proxy_redirect指令: proxy_redirect http://test.abc.com:9080/ / ,把所有“http://test.abc.com:9080/”的内容替换成“/”再发给客户端,就解决了。
# server {
# listen 80;
# server_name test.abc.com;
# proxy_redirect http://test.abc.com:9080/ /;
# location / {
# proxy_pass http://10.10.10.1:9080;
# }
# }
#
proxy_redirect http://192.168.1.85:8080/jsse/ /;
# 例子,有如下匹配规则:
# location = / {
# #规则A
# }
# location = /login {
# #规则B
# }
# location ^~ /static/ {
# #规则C
# }
# location ~ \.(gif|jpg|png|js|css)$ {
# #规则D
# }
# location ~* \.png$ {
# #规则E
# }
# location !~ \.xhtml$ {
# #规则F
# }
# location !~* \.xhtml$ {
# #规则G
# }
# location / {
# #规则H
# }
# 那么产生的效果如下:
# 访问根目录/, 比如http://localhost/ 将匹配规则A
# 访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H
# 访问 http://localhost/static/a.html 将匹配规则C
# 访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用,而 http://localhost/static/c.png 则优先匹配到规则C
# 访问 http://localhost/a.PNG 则匹配规则E,而不会匹配规则D,因为规则E不区分大小写。
# 访问 http://localhost/a.xhtml 不会匹配规则F和规则G,http://localhost/a.XHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。
# 访问 http://localhost/category/id/1111 则最终匹配到规则H,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。
# 防盗链
# location ~* \.(gif|jpg|swf)$ {
# valid_referers none blocked start.igrow.cn sta.igrow.cn;
# if ($invalid_referer) {
# rewrite ^/ http://$host/logo.png;
# }
# }
# 六、根据文件类型设置过期时间
# location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
# if (-f $request_filename) {
# expires 1h;
# break;
# }
# }
# 七、禁止访问某个目录
# location ~* \.(txt|doc)${
# root /data/www/wwwroot/linuxtone/test;
# deny all;
# }
location / {
# 转发地址
proxy_pass http://localhost:8484;
proxy_redirect default;
proxy_set_header Host $host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /jsse/ {
proxy_pass http://192.168.1.85:8080/jsse/;
proxy_redirect default;
proxy_set_header Host $host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 8038;
server_name localhost;
#charset koi8-r;
#access_log logs/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配置》,
请注明出处:https://www.zhoulujun.cn/html/tools/webServer/nginx/2016_0229_7652.html