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;
        }
    }
}
相关推荐
coisini.cn3 分钟前
Windows10、CentOS Stream9 环境下安装kafka_2.12-3.6.2记录
运维·zookeeper·kafka·windows10·centos stream 9
秋秋秋叶16 分钟前
Python学习——【3.1】函数
python·学习
路溪非溪27 分钟前
Linux内核启动流程
linux·运维·服务器
anddddoooo1 小时前
vulnhub(11):derpnstink(hydra爆破用户名和密码、验证的文件上传)
linux·运维·服务器·安全·web安全·网络安全
Xinan_____1 小时前
Linux——k8s认识
linux·运维·kubernetes
TO_ZRG1 小时前
使用jenkins打包unity工程
运维·unity·jenkins
liujiangxu1 小时前
jenkins声明式流水线语法详解
运维·自动化·jenkins
攸攸太上1 小时前
Docker学习
java·网络·学习·docker·容器
Ylucius2 小时前
JavaScript 与 Java 的继承有何区别?-----原型继承,单继承有何联系?
java·开发语言·前端·javascript·后端·学习
哦豁灬2 小时前
NCNN 学习(1)-编译与算子注册
深度学习·学习·ncnn