ngnix的405错误

nginx遇到post请求静态文件会得到405错误 
用upstream 把post转成get方式 



添加 nginx.conf 
Java代码   收藏代码
  1. 添加  
  2. upstream static_backend {  
  3.   server localhost:80;  
  4. }  
  5. server {  
  6. ....  
  7. 添加  
  8.         error_page 405 =200 @405;  
  9.     location @405 {  
  10.         root html;  
  11.         proxy_method GET;  
  12.         proxy_pass http://static_backend;  
  13.     }  

或者 
Java代码   收藏代码
  1. location / {        
  2.             root   html;        
  3.             index  index.html index.htm index.php;                                                              
  4.             error_page 405 =200 http://$host$request_uri;  
  5.         }     

完整的如下: 
Java代码   收藏代码
  1. #user  nobody;  
  2. worker_processes  1;  
  3.   
  4. #error_log  logs/error.log;  
  5. #error_log  logs/error.log  notice;  
  6. #error_log  logs/error.log  info;  
  7.   
  8. #pid        logs/nginx.pid;  
  9.   
  10.   
  11. events {  
  12.     worker_connections  1024;  
  13. }  
  14.   
  15.   
  16. http {  
  17.     include       mime.types;  
  18.     default_type  application/octet-stream;  
  19.   
  20.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  21.     #                  '$status $body_bytes_sent "$http_referer" '  
  22.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  23.   
  24.     #access_log  logs/access.log  main;  
  25.   
  26.     sendfile        on;  
  27.     #tcp_nopush     on;  
  28.   
  29.     #keepalive_timeout  0;  
  30.     keepalive_timeout  65;  
  31.   
  32.     #gzip  on;  
  33.     upstream static_backend {  
  34.         server localhost:80;  
  35.     }  
  36.     server {  
  37.         listen       80;  
  38.         server_name  localhost;  
  39.   
  40.         #charset koi8-r;  
  41.   
  42.         #access_log  logs/host.access.log  main;  
  43.   
  44.         location / {  
  45.             root   html;  
  46.             index  index.html index.htm;  
  47.         }  
  48.   
  49.         #error_page  404              /404.html;  
  50.   
  51.         # redirect server error pages to the static page /50x.html  
  52.         #  
  53.         error_page   500 502 503 504  /50x.html;  
  54.         location = /50x.html {  
  55.             root   html;  
  56.         }  
  57.         error_page 405 =200 @405;  
  58.     location @405 {  
  59.         root html;  
  60.         proxy_method GET;  
  61.         proxy_pass http://static_backend;  
  62.     }  
  63.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  64.         #  
  65.         #location ~ \.php$ {  
  66.         #    proxy_pass   http://127.0.0.1;  
  67.         #}  
  68.   
  69.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  70.         #  
  71.         #location ~ \.php$ {  
  72.         #    root           html;  
  73.         #    fastcgi_pass   127.0.0.1:9000;  
  74.         #    fastcgi_index  index.php;  
  75.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  76.         #    include        fastcgi_params;  
  77.         #}  
  78.   
  79.         # deny access to .htaccess files, if Apache's document root  
  80.         # concurs with nginx's one  
  81.         #  
  82.         #location ~ /\.ht {  
  83.         #    deny  all;  
  84.         #}  
  85.     }  
  86.   
  87.   
  88.     # another virtual host using mix of IP-, name-, and port-based configuration  
  89.     #  
  90.     #server {  
  91.     #    listen       8000;  
  92.     #    listen       somename:8080;  
  93.     #    server_name  somename  alias  another.alias;  
  94.   
  95.     #    location / {  
  96.     #        root   html;  
  97.     #        index  index.html index.htm;  
  98.     #    }  
  99.     #}  
  100.   
  101.   
  102.     # HTTPS server  
  103.     #  
  104.     #server {  
  105.     #    listen       443;  
  106.     #    server_name  localhost;  
  107.   
  108.     #    ssl                  on;  
  109.     #    ssl_certificate      cert.pem;  
  110.     #    ssl_certificate_key  cert.key;  
  111.   
  112.     #    ssl_session_timeout  5m;  
  113.   
  114.     #    ssl_protocols  SSLv2 SSLv3 TLSv1;  
  115.     #    ssl_ciphers  HIGH:!aNULL:!MD5;  
  116.     #    ssl_prefer_server_ciphers   on;  
  117.   
  118.     #    location / {  
  119.     #        root   html;  
  120.     #        index  index.html index.htm;  
  121.     #    }  
  122.     #}  
  123.   
  124. }  


a.c的代码 
C代码   收藏代码
  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5. #include   
  6.   
  7. int main(argc,argv){  
  8.     int s,len;  
  9.     struct sockaddr_in remote_addr;  
  10.     char buf[BUFSIZ];  
  11.     memset(&remote_addr,0,sizeof(remote_addr));  
  12.     remote_addr.sin_family=AF_INET;  
  13.     remote_addr.sin_addr.s_addr=inet_addr("127.0.0.1");  
  14.     remote_addr.sin_port=htons(8000);  
  15.         if((s=socket(AF_INET,SOCK_STREAM,0))<0)  
  16.     {  
  17.         perror("socket");  
  18.         return 1;  
  19.     }  
  20.     if(connect(s,(struct sockaddr *)&remote_addr,sizeof(struct sockaddr))<0)  
  21.     {  
  22.         perror("connect");  
  23.         return 1;  
  24.     }  
  25.     printf("connect to server\n");  
  26.     len=recv(s,buf,BUFSIZ,0);  
  27.     buf[len]='\0';  
  28.     printf("%s",buf);  
  29.     while(1){  
  30.         printf("enter string to end:");  
  31.         scanf("%a",buf);  
  32.         if(!strcmp(buf,"quit"))  
  33.             break;  
  34.         len=send(s,buf,strlen(buf),0);  
  35.         len=recv(s,buf,strlen(buf),0);  
  36.         buf[len]='\0';  
  37.         printf(" received:%s\n",buf);  
  38.     }  
  39.     close(s);  
  40.     return 0;  
  41. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值