经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Nginx » 查看文章
Nginx一些基本配置
来源:cnblogs  作者:ColorsWin  时间:2021/6/21 10:08:03  对本文有异议

Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。如果网站内容是html的,我经常用它来替代IIS或tomcat服务器。

简单总结一下本地文件基本配置,方便拷贝。

server {

  1. listen 80;
  2. server_name localhost;
  3. #charset koi8-r;
  4. #access_log logs/host.access.log main;
  5. root pages/home;#项目的打包后的目录
  6. index loading.html; #项目的起始页
  7. #对应上面的@router,主要原因是路由的路径资源并不是一个真实的路径,所以无法找到具体的文件
  8. #因此需要rewrite到index.html中,然后交给路由在处理请求资源
  9. location @router {
  10. rewrite ^.*$ /index.html last;
  11. }
    }

1、不同端口 映射不同路径,只需要多配置一个server。 

  修改listen 端口和 root 对应文件目录

2、多个域名用一个nginx,映射到不同文件目录

新增一个server 节点 ,端口设置一样,然后修改 server_name即可

listen 80;

server_name www.multicolorwin.com;

注意 server_name的写法,如果只写www.AAA.com 那么在输入 AAA.com时候会跳转其他节点的页面。

因此需要配置多个 server_name  AAA.com www.AAA.com也支持正则表达式写法

 

完整配置如下:

  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 80;
  24. server_name localhost;
  25. #charset koi8-r;
  26. #access_log logs/host.access.log main;
  27. root pages/colorswin;#项目的打包后的目录
  28. index loading.html; #项目的起始页
  29. #对应上面的@router,主要原因是路由的路径资源并不是一个真实的路径,所以无法找到具体的文件
  30. #因此需要rewrite到index.html中,然后交给路由在处理请求资源
  31. location @router {
  32. rewrite ^.*$ /index.html last;
  33. }
  34. #error_page 404 /404.html;
  35. # redirect server error pages to the static page /50x.html
  36. #
  37. error_page 500 502 503 504 /50x.html;
  38. location = /50x.html {
  39. root html;
  40. }
  41. }
  42. server {
  43. listen 80;
  44. server_name multicolorwin.com www.multicolorwin.com;
  45. #charset koi8-r;
  46. #access_log logs/host.access.log main;
  47. root pages/multicolorwin;#vue项目的打包后的dist
  48. location / {
  49. try_files $uri $uri/ @router;#需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404
  50. index index.html index.htm;
  51. }
  52. #对应上面的@router,主要原因是路由的路径资源并不是一个真实的路径,所以无法找到具体的文件
  53. #因此需要rewrite到index.html中,然后交给路由在处理请求资源
  54. location @router {
  55. rewrite ^.*$ /index.html last;
  56. }
  57. #error_page 404 /404.html;
  58. # redirect server error pages to the static page /50x.html
  59. #
  60. error_page 500 502 503 504 /50x.html;
  61. location = /50x.html {
  62. root html;
  63. }
  64. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  65. #
  66. #location ~ \.php$ {
  67. # proxy_pass http://127.0.0.1;
  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. # deny access to .htaccess files, if Apache's document root
  79. # concurs with nginx's one
  80. #
  81. #location ~ /\.ht {
  82. # deny all;
  83. #}
  84. }
  85. # another virtual host using mix of IP-, name-, and port-based configuration
  86. #
  87. #server {
  88. # listen 8000;
  89. # listen somename:8080;
  90. # server_name somename alias another.alias;
  91. # location / {
  92. # root html;
  93. # index index.html index.htm;
  94. # }
  95. #}
  96. # HTTPS server
  97. #
  98. #server {
  99. # listen 443 ssl;
  100. # server_name localhost;
  101. # ssl_certificate cert.pem;
  102. # ssl_certificate_key cert.key;
  103. # ssl_session_cache shared:SSL:1m;
  104. # ssl_session_timeout 5m;
  105. # ssl_ciphers HIGH:!aNULL:!MD5;
  106. # ssl_prefer_server_ciphers on;
  107. # location / {
  108. # root html;
  109. # index index.html index.htm;
  110. # }
  111. #}
  112. }

 

原文链接:http://www.cnblogs.com/ColorsWin/p/14889091.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号