Working with Nginx

All command have to run at nginx.exe folder.

start nginx	// start, at the working folder where nginx.exe located
stop nginx	// stop

nginx -s stop	// fast shutdown
nginx -s quit	// graceful shutdown
nginx -s reload	//changing configuration, starting new worker processes with a new configuration, graceful shutdown of old worker processes
nginx -s reopen	//re-opening log files

tasklist /fi "imagename eq nginx.exe"	// check current nginx instance
taskkill /f /IM nginx.exe	// kill all nginx progress

An example of nginx.conf


#user  nobody;
worker_processes  auto;

#error_log  logs/error.log;
#pid        logs/nginx.pid;

worker_rlimit_nofile 8192;

events {
    worker_connections  8000;
}

http {
    server_names_hash_bucket_size  64;

    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        listen       443 ssl;
        server_name  test.divogy.com;

        #https certificate configuration
        ssl_certificate      cert/test.divogy.com.pem;
        ssl_certificate_key  cert/test.divogy.com.key;
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
        



        #config Security Policy -> Bind Domain. This example based on requirement of Shopify.
        add_header Content-Security-Policy "frame-ancestors 'https://$host https://*.myshopify.com https://admin.shopify.com'; upgrade-insecure-requests" always;
        #add_header Content-Security-Policy "default-src 'self'; script-src 'self' cdn.shopify.com cdn.shopifycloud.com; style-src 'self' cdn.shopify.com cdn.shopifycloud.com 'unsafe-inline'; img-src 'self' cdn.shopify.com cdn.shopifycloud.com v.shopify.com data:; font-src 'self' cdn.shopify.com cdn.shopifycloud.com data:; frame-ancestors 'self' 'https://test.divogy.com https://*.myshopify.com https://admin.shopify.com'; upgrade-insecure-requests" always;

        location / {
            proxy_pass http://127.0.0.1:12345;
            index index.js;
            
            #proxy pass directly
            proxy_pass http://localhost:46892;
            proxy_set_header Host pinogy.divogy.com;
            proxy_http_version 1.1;
            #proxy_set_header Upgrade $http_upgrade;
            #proxy_set_header Connection 'upgrade';
            #proxy_cache_bypass $http_upgrade;
        }
    }

    # default root html
    server {
        listen       80;
        server_name  www.divogy.com;
        location / {
            root   html;
            index  index.html index.htm;
        }
    }
}

See demo server configuration in nginx.conf

    server {
        listen       80;
        server_name  db.mysql.divogy.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        #location = /50x.html {
        #    root   html;
        #}

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

Set auto-startup with WinSW.exe

Windows 可以通过手动修改注册表设置启动项,感觉有些麻烦,还是找个工具。找到工具 WinSW ,它可以将任何应用包裹并作为一个 Windows 服务管理。

WinSW 作为一个全局工具使用:

  1. 下载 WinSW.exe 或 WinSW.zip 。
  2. 新建 myapp.xml
  3. 运行 winsw install myapp.xml [options] 安装服务。
  4. 运行 winsw start myapp.xml 开启服务。
  5. 运行 winsw status myapp.xml 检查服务是否启动和运行。
<service>
  <id>nginx service</id>
  <name>Nginx</name>
  <description>This service runs Nginx.</description>
  <env name="NGINX_COMIC" value="%BASE%" />
  <prestart>start D:\nginx\nginx.exe</prestart>
  <executable>D:\nginx\nginx.exe</executable>
  <prestop>D:\nginx\nginx.exe -s stop</prestop>
  <log mode="roll" />
  <onfailure action="none" />
</service>
  1. 拷贝WinSW.exe到Nginx文件夹
  2. 新建WinSW.xml文件,并加入以上配置
  3. 管理员运行WinSW.exe将服务加入services.msc中

Leave a Comment

Your email address will not be published.