Nginx学习与使用记录

定义域名(本地)

本地定义域名不需要证书,直接更改hosts文件。
注意:在这个文件夹中是无法更改hosts文件的,可以将hosts复制到桌面进行更改后再覆盖原先的hosts文件
win路径:C:\Windows\System32\drivers\etc

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
127.0.0.1       localhost
#	::1             localhost
127.0.0.1       activate.navicat.com
127.0.0.1       ts5nginx.com

Nginx的一下常用命令记录

启动
	start nginx
关闭
	nginx.exe -s stop
强停
	taskkill /f /t /im nginx.exe
重启()
	nginx -s reload

win系统使用 .bat来启动

新建一个txt文本在txt文本中写入启动或关闭等命令,然后修改后缀名 .bat。在要使用某个命令时点击一下就可以了。
注意:文本要建在nginx.exe同类目下

start nginx

nginx配置

#进程用户
#user  nobody;

#工作进程,配合和CPU个数保持一致
worker_processes  2;

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

#pid        logs/nginx.pid;


events {
    #内核模型,select、poll、epoll
    #use epoll;

    #每个 worker process 可以同时支持的最大连接数等。
    worker_connections  1024;
}


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

    # 日志打印的内容格式
    # 注释时,默认打印的是 127.0.0.1 - - [23/May/2024:10:28:21 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
    # 自定义后  会将请求头,请求类型等打印出来
    #127.0.0.1 - - [23/May/2024:15:51:02 +0800] "POST /wxapi.php/Device/doGetStatus HTTP/1.1" 200 157 "http://ts5nginx.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
    # 注意!!! 只使用这个log_format是无效的必须 access_log的路径必须要写否则还是默认的
    log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"'
                        '"$server_name"';

    # win 的log日志地址需要绝对路径, 在http中定义那就是所有服务(server)的可以在每个服务(server)中单独定义 access_log
    access_log  C:/Work/nginx-1.26.0/logs/access.log;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        # 80端口不可更改否则访问时必须带端口,例如81 正常:http://localhost/ 更改后:http://localhost:81/
        # 可能是因为防火墙的原因
        listen       80;
        #本地域名在hosts文件中添加。注意在这个文件夹中无法修改hosts文件需要复制一个文件到可修改的文件夹中进行修改然后复制回来覆盖掉老的文件   C:\Windows\System32\drivers\etc
        server_name  ts5nginx.com;

        # 默认重定向 使用ts5nginx.com 来访问就定向到C:/Work_file/tian/Web/tvue/dist中 并尝试查找并返回匹配的文件。具体地说,它会首先查找index.php文件,如果不存在则查找index.html文件,如果还不存在则查找index.htm文件。
        # 注意路径要绝对路径,win与linux都是绝对路径。win的路径不能使用\斜杠
        root   C:/Work_file/tian/Web/tvue/dist;
        index  index.php index.html index.htm;

        # 将所有的访问转换成 http 正式就用 https://$host$request_uri 转换成https
        return 301   http://$host$request_uri;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        # 重定向到服务器的 8081服务(后端)
        location ^~/wxapi.php/ {
            proxy_pass http://localhost:8081;
            proxy_set_header Host $host$request_uri;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            root html;
            index  index.html index.htm;
        }

        # 使用 ts5nginx.com/img.png 便可访问服务器上的文件
        # 用于微信的域名证书验证什么的 MP_verify_0EZJXnKP7GfACeli.txt
        # 例: location /MP_verify_0EZJXnKP7GfACeli.txt {
        #       alias   C:\Users\DELL\Pictures\MP_verify_0EZJXnKP7GfACeli.txt;
        #    }
        location /img.png {
            alias   C:\Users\DELL\Pictures\1715390062731.png;
        }

        # 默认报错时展示的页面
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值