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;
        }
    }
}
相关推荐
知白守黑2671 小时前
Ansible角色
运维·服务器·ansible
Jwest20211 小时前
工业显示器在地铁电力监控与运维中的应用
运维·计算机外设
知识分享小能手1 小时前
React学习教程,从入门到精通, React 属性(Props)语法知识点与案例详解(14)
前端·javascript·vue.js·学习·react.js·vue·react
茯苓gao4 小时前
STM32G4 速度环开环,电流环闭环 IF模式建模
笔记·stm32·单片机·嵌入式硬件·学习
是誰萆微了承諾4 小时前
【golang学习笔记 gin 】1.2 redis 的使用
笔记·学习·golang
DKPT5 小时前
Java内存区域与内存溢出
java·开发语言·jvm·笔记·学习
aaaweiaaaaaa5 小时前
HTML和CSS学习
前端·css·学习·html
fuyongliang1236 小时前
nginx反向代理,负载均衡,tomcat的数据流向图篇解析
nginx·tomcat·负载均衡
看海天一色听风起雨落6 小时前
Python学习之装饰器
开发语言·python·学习
tuokuac6 小时前
nginx配置前端请求转发到指定的后端ip
前端·tcp/ip·nginx