Nginx 反向代理配置

曾经终败给现在 2022-11-25 04:14 368阅读 0赞

什么是nginx

  • Nginx是一款轻量级Web服务器,也是一款反向代理服务器
  • 官网:http://nginx.org/
  • 中文文档: http://www.nginx.cn/doc/

nginx可以提供的服务有:

**1. web 服务

  1. 负载均衡 (反向代理)
  2. web cache(web 缓存**)

nginx常用命令

  • 测试配置文件:${Nginx}/sbin/nginx -t
  • 启动命令:${Nginx}/sbin/nginx
  • 停止命令:${Nginx}/sbin/nginx -s stop/quit
  • 重启命令:${Nginx}/sbin/nginx -s reload
  • 查看进程命令:ps -ef | grep nginx
  • 平滑重启:kill -HUP [Nginx主进程号(即ps命令查到的PID)]

nginx实现反向代理配置

1.打开 ${Nginx}/conf/nginx.conf 文件

  1. worker_processes 1;
  2. events {
  3. worker_connections 1024;
  4. }
  5. http {
  6. include mime.types;
  7. default_type application/octet-stream;
  8. sendfile on;
  9. # 这个server配置表示访问localhost:8081地址 将请求转发到这个server下定义的location位置上
  10. server {
  11. listen 8081;
  12. server_name localhost;
  13. # 这个location表示将localhost:8081地址转发到html文件夹下的index2.html首页上
  14. location / {
  15. root html;
  16. index index2.html index.htm;
  17. }
  18. }
  19. # 这个server表示访问localhost:80地址 将请求转发到这个server下定义的location位置上
  20. server {
  21. listen 80;
  22. server_name localhost;
  23. # 这个location表示将localhost:80请求 转发到index.html文件上
  24. location / {
  25. root html;
  26. index index.html index.htm;
  27. }
  28. #location / {
  29. #proxy_pass http://www.zzdy.wang:18537/;
  30. # }
  31. #这个location表示将localhost:80/demo/开头的请求 转发到http://localhost:8080/服务上
  32. location /demo {
  33. proxy_pass http://localhost:8080/;
  34. }
  35. error_page 500 502 503 504 /50x.html;
  36. location = /50x.html {
  37. root html;
  38. }
  39. }
  40. # nginx:http转发到https
  41. HTTPS server
  42. server {
  43. listen 8069;
  44. server_name www.zzdy**.cn;
  45. rewrite ^(.*)$ https://$host$1 permanent;
  46. }
  47. }
  48. # 在做请求转发时,一定要注意proxy_pass后/符号的添加,不加/表示访问 http://localhost:8080/demo+ 加/表示访问http://localhost:8080/+

发表评论

表情:
评论列表 (有 0 条评论,368人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Nginx配置反向代理

    一、前言 反向代理作用 隐藏服务器信息 -> 保证内网的安全,通常将反向代理作为公网访问地址,web服务器是内网,即通过nginx配置外网访问web服务器内网 举例