[关闭]
@liruiyi962464 2025-01-21T17:17:08.000000Z 字数 4892 阅读 72

Nginx启动前端(设置为自启动

windows部署


1:Nginx配置文件nginx.conf

  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 9527;
  24. server_name localhost;
  25. #charset koi8-r;
  26. #access_log logs/host.access.log main;
  27. location / {
  28. root .\html\dist;
  29. index index.html index.htm;
  30. }
  31. #error_page 404 /404.html;
  32. # redirect server error pages to the static page /50x.html
  33. #
  34. error_page 500 502 503 504 /50x.html;
  35. location = /50x.html {
  36. root html;
  37. }
  38. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  39. #
  40. #location ~ \.php$ {
  41. # proxy_pass http://127.0.0.1;
  42. #}
  43. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  44. #
  45. #location ~ \.php$ {
  46. # root html;
  47. # fastcgi_pass 127.0.0.1:9000;
  48. # fastcgi_index index.php;
  49. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  50. # include fastcgi_params;
  51. #}
  52. # deny access to .htaccess files, if Apache's document root
  53. # concurs with nginx's one
  54. #
  55. #location ~ /\.ht {
  56. # deny all;
  57. #}
  58. }
  59. # another virtual host using mix of IP-, name-, and port-based configuration
  60. #
  61. #server {
  62. # listen 8000;
  63. # listen somename:8080;
  64. # server_name somename alias another.alias;
  65. # location / {
  66. # root html;
  67. # index index.html index.htm;
  68. # }
  69. #}
  70. # HTTPS server
  71. #
  72. #server {
  73. # listen 443 ssl;
  74. # server_name localhost;
  75. # ssl_certificate cert.pem;
  76. # ssl_certificate_key cert.key;
  77. # ssl_session_cache shared:SSL:1m;
  78. # ssl_session_timeout 5m;
  79. # ssl_ciphers HIGH:!aNULL:!MD5;
  80. # ssl_prefer_server_ciphers on;
  81. # location / {
  82. # root html;
  83. # index index.html index.htm;
  84. # }
  85. #}
  86. }

2:设置自启动,下载Windows Service Wrapper工具

下载地址https://github.com/winsw/winsw/releas
sample-minimal.xml以及WinSW-x64.exe

  • 1:将下载的两个文件放到 nginx 的安装目录下,并且将其重命名为 nginx-service.exe、nginx-service.xml。
  • 2:在 nginx 安装目录下新建服务日志文件夹 server-logs 文件夹,存放nginx服务相关日志。
  • 3:修改 nginx-service.xml 文件,写入配置信息,就可将nginx注册为Windows服务

1:设置自启动,下载Windows Service Wrapper工具

下载地址https://github.com/winsw/winsw/releas
sample-minimal.xml以及WinSW-x64.exe

  • 1:将下载的两个文件放到 nginx 的安装目录下,并且将其重命名为 nginx-service.exe、nginx-service.xml。
  • 2:在 nginx 安装目录下新建服务日志文件夹 server-logs 文件夹,存放nginx服务相关日志。
  • 3:修改 nginx-service.xml 文件,写入配置信息,就可将nginx注册为Windows服务

nginx-service.xml

  1. <!-- nginx-service.xml -->
  2. <service>
  3. <!-- 服务的唯一标识符,用于系统内部识别 -->
  4. <id>nginx</id>
  5. <!-- 服务显示名称,通常出现在服务列表中 -->
  6. <name>nginx</name>
  7. <!-- 服务的描述信息,提供关于服务功能的简短说明 -->
  8. <description>这是一个基于Nginx的服务。</description>
  9. <!-- 日志文件存放路径,指定Nginx将日志写入的具体位置 -->
  10. <logpath>D:\fangzhen\server-logs\</logpath>
  11. <!-- 日志模式,这里设置为"roll"意味着当达到一定大小时,日志文件会滚动更新 -->
  12. <logmode>roll</logmode>
  13. <!-- 启动服务时执行的命令或程序路径 -->
  14. <executable>D:\fangzhen\nginx.exe</executable>
  15. <!-- 停止服务时执行的命令或程序路径 -->
  16. <stopexecutable>D:\fangzhen\nginx.exe -s stop</stopexecutable>
  17. </service>
  • 4:start.bat文件,一键安装nginx服务【以管理员身份运行】
  1. @echo off
  2. cd /d D:\fangzhen
  3. nginx-service.exe install
  4. sc config nginx start= auto
  5. sc start nginx
  6. echo Nginx服务已安装并已启动。
  7. pause
  • 5:stop.bat文件,一键删除nginx服务【以管理员身份运行】
  1. @echo off
  2. sc stop nginx
  3. sc delete nginx
  4. echo Nginx服务已删除。
  5. pause

nginx-service.xml

  1. <!--
  2. MIT License
  3. Copyright (c) 2008-2020 Kohsuke Kawaguchi, Sun Microsystems, Inc., CloudBees,
  4. Inc., Oleg Nenashev and other contributors
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in all
  12. copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.
  20. -->
  21. <!--
  22. This is an example of a minimal Windows Service Wrapper configuration, which includes only mandatory options.
  23. This configuration file should be placed near the WinSW executable, the name should be the same.
  24. E.g. for myapp.exe the configuration file name should be myapp.xml
  25. You can find more information about the configuration options here: https://github.com/kohsuke/winsw/blob/master/doc/xmlConfigFile.md
  26. Full example: https://github.com/kohsuke/winsw/blob/master/examples/sample-allOptions.xml
  27. -->
  28. <service>
  29. <!-- 服务的唯一标识符,用于系统内部识别 -->
  30. <id>nginx</id>
  31. <!-- 服务显示名称,通常出现在服务列表中 -->
  32. <name>nginx</name>
  33. <!-- 服务的描述信息,提供关于服务功能的简短说明 -->
  34. <description>这是一个基于Nginx的服务。</description>
  35. <!-- 日志文件存放路径,指定Nginx将日志写入的具体位置 -->
  36. <logpath>D:\fangzhen\server-logs\</logpath>
  37. <!-- 日志模式,这里设置为"roll"意味着当达到一定大小时,日志文件会滚动更新 -->
  38. <logmode>roll</logmode>
  39. <!-- 启动服务时执行的命令或程序路径 -->
  40. <executable>D:\fangzhen\nginx.exe</executable>
  41. <!-- 停止服务时执行的命令或程序路径 -->
  42. <stopexecutable>D:\fangzhen\nginx.exe -s stop</stopexecutable>
  43. </service>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注