Nginx只允许某些URL的POST请求

前端之家收集整理的这篇文章主要介绍了Nginx只允许某些URL的POST请求前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个应用程序将使用GET& POST方法.为了更好的安全性,我已将Nginx配置为仅使用POST请求来服务页面.下面是我在Nginx中使用的配置.

Nginx中配置:
if($request_method!〜^(POST)$){
 返回404; }

这很完美.
现在,我想改变Nginx中的上述配置,以便同时使用GET和amp; POST请求.但是,我无法做到.

我使用了很多组合,但没有运气.

有人可以帮我配置相同的Nginx.

下面是我的Nginx配置文件.

注意:我使用Nginx(在前端)作为web服务器和apache(在后端)用于服务应用程序.我已经配置了Nginx来成功地将请求的网页重定向到apache.

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

提前致谢,
桑迪普

最佳答案
我会写这样的东西:

  1. location /application {
  2. proxy_pass http://

变化:

>几乎没有理由写limit_except GET POST. A认为禁止对这些地址的OPTIONS请求对您来说很重要.
>你真的想要像/ APPLICATION / Pd /这样的网址吗?我不这么认为,我已经改变了〜*〜.
>从proxy_pass中删除了路径部分,因此Nginx将代理原始路径.
>删除了指定位置.

猜你在找的Nginx相关文章