Nginx命令

命令

启动nginx
start nginx
查看是否存在进程
tasklist /fi "imagename eq nginx.exe"
查看进程号
ps aux|grep nginx 重新加载配置文件 nginx -s reload 快速关闭 nginx -s stop 有序关闭 nginx -s quit
查看版本号
./nginx -v
nginx.conf文件

#user nobody;
#==工作进程数,一般设置为cpu核心数
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {

#==最大连接数,一般设置为cpu*2048
worker_connections 1024;
}


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

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;

#==客户端链接超时时间
keepalive_timeout 65;

#gzip on;

#当配置多个server节点时,默认server names的缓存区大小就不够了,需要手动设置大一点
server_names_hash_bucket_size 512;

#server表示虚拟主机可以理解为一个站点,可以配置多个server节点搭建多个站点
#每一个请求进来确定使用哪个server由server_name确定

server{

  listen       8081;   #监听端口,基于IP配置的时候变更此处
  server_name  www.xdw.com;  #主机域名,实际项目发布的话,填公网上的域名,本地部署的话,可以在C:WindowsSystem32driversetchosts文件中添加IP和域名的映射
  location ^~/ {      #映射解析
     root   E:/xdw/0221;   #工程所在路径
   charset  utf-8;
     index  index.html index.htm;  #首页(默认页)
     proxy_pass http: //127.0.0.1:8081;   #转发后端站点地址,一般用于做软负载,轮询后端服务器。请求转向
     alias D:/profile #转发静态资源
     deny 127.0.0.1 拒绝的ip
     allow 127.0.0.1 允许的ip
     add_header 'Access-Control-Allow-Origin' '*';
     add_header 'Access-Control-Allow-Credentials' 'true';
     add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
     add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
     #重新定义或者添加发往后端服务器的请求头
     #给请求头中添加客户请求主机名
     proxy_set_header Host $host;
     #给请求头中添加客户端IP
     proxy_set_header X-Real-IP $remote_addr;
     #将$remote_addr变量值添加在客户端“X-Forwarded-For”请求头的后面,并以逗号分隔。 如果客户端请求未携带“X-Forwarded-For”请求头,$proxy_add_x_forwarded_for变量值将与$remote_addr变量相同  
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     #给请求头中添加客户端的Cookie
     proxy_set_header Cookie $http_cookie;
     #将使用代理服务器的主域名和端口号来替换。如果端口是80,可以不加。
     proxy_redirect off;
            
     #浏览器对 Cookie 有很多限制,如果 Cookie 的 Domain 部分与当前页面的 Domain 不匹配就无法写入。
     #所以如果请求 A 域名,服务器 proxy_pass 到 B 域名,然后 B 服务器输出 Domian=B 的 Cookie,
     #前端的页面依然停留在 A 域名上,于是浏览器就无法将 Cookie 写入。
            
    #不仅是域名,浏览器对 Path 也有限制。我们经常会 proxy_pass 到目标服务器的某个 Path 下,
     #不把这个 Path 暴露给浏览器。这时候如果目标服务器的 Cookie 写死了 Path 也会出现 Cookie 无法写入的问题。
            
     #设置“Set-Cookie”响应头中的domain属性的替换文本,其值可以为一个字符串、正则表达式的模式或一个引用的变量
     #转发后端服务器如果需要Cookie则需要将cookie domain也进行转换,否则前端域名与后端域名不一致cookie就会无法存取
   #配置规则:proxy_cookie_domain serverDomain(后端服务器域) nginxDomain(nginx服务器域)
     proxy_cookie_domain localhost .testcaigou800.com;
            
     #取消当前配置级别的所有proxy_cookie_domain指令
     #proxy_cookie_domain off;
     #与后端服务器建立连接的超时时间。一般不可能大于75秒;
     proxy_connect_timeout 30;
  }
 }               

SSL配置

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
 worker_connections  1024;
}


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

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;


    server {
        listen  443;
        server_name XXXX.com; #网站 生产环境
#
#        # ssl证书地址
        ssl_certificate     C:/javaService/XXXX.com_nginx/XXXX.com_bundle.pem;  # pem文件的路径
        ssl_certificate_key  C:/javaService/XXXX.com_nginx/XXXX.com.key; # key文件的路径

        # ssl验证相关配置
        ssl_session_timeout 5m; #缓存有效期
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;        #加密算法
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;    #安全链接可选的加密协议
        ssl_prefer_server_ciphers on;   #使用服务器端的首选算法
        root C:/javaService/nginx-1.23.1/html;
        index index.html index.htm;

        location / {
            try_files $uri $uri/ /index.html;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
                expires      30d;
        }

        location ~ .*\.(js|css)?$ {
                expires      12h;
        }
    }
    server {
        listen  80;
        server_name afghan-gandhara.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;
    #    }
    #}
}

 

负载均衡

#user  nobody;
worker_processes  1;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
   upstream webservers{
      server  192.168.9.134:8081 weight=8;
      server  192.168.9.134:8082 weight=2;
   }
 
    server {
        listen       80;
        server_name  localhost;
        #location / {
         #   root   html;
          #  index  index.html index.htm;
        #}

        location / {
             #转发到负载服务上
            proxy_pass http://webservers/api/;
         }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

 

posted @ 2022-09-23 10:26  Sjh_code  阅读(226)  评论(0编辑  收藏  举报