nginx随笔

1、虽然 docker-compose ps 显示端口映射正常,但请求未到达 Nginx,且 curl localhost 未触发日志输出

在nginx.conf配置项中增加了 [Nginx] 打印,然后docker-compose logs 可以看到日志有输出,说明之前的理解有误,不是请求未达到Nginx,而是log的日志输出我没看懂

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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

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

    #access_log  /var/log/nginx/access.log  main;

    error_log /dev/stderr;  # ....................................
    access_log /dev/stdout main;  # ....................................

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
~

日志输出

email-system              | [Nginx]172.18.0.1 - - [16/May/2025:08:40:42 +0000] "GET /?timestep=1747384842968 HTTP/1.1" 200 4905 "http://email-server/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:138.0) Gecko/20100101 Firefox/138.0" "-"
email-system              | [Nginx]10.5.1.88 - - [16/May/2025:08:40:56 +0000] "GET /?timestep=1747384856260 HTTP/1.1" 200 4905 "http://10.5.1.130/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36" "-"
email-system              | [Nginx]10.5.1.88 - - [16/May/2025:08:40:56 +0000] "GET /?timestep=1747384856260 HTTP/1.1" 200 4905 "http://10.5.1.130/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36" "-"

2、cors的问题用nginx已解决,修改方式是改配置如下
conf修改如下

root@host-2288H-V5:/home/host/email_project/邮件系统# vim /home/developer/data/nginx/default.conf

server {
    listen       80;
    listen  [::]:80;
    server_name  _;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        add_header 'Access-Control-Allow-Origin' '*' always;
    }


    # 代理 /api/metric 到 http://java-metric
    location ^~/goemail/ {
        proxy_pass http://10.5.1.130:81/;
        proxy_set_header Host $host;
        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;
    }

hosts文件修改如下

# 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


192.168.218.1 windows10.microdone.cn
10.5.1.130 go-email
10.5.1.130 web-email
10.5.1.130 email-server
10.5.1.130 email-server.lan
10.5.1.130 java-metric
10.5.1.130 java-translate
10.5.1.130 java-translate.system
10.5.1.130 go-tel
10.5.1.130 java-container
10.5.1.130 python-email
10.5.1.130 go-pro1
10.5.1.130 go-pro2
10.5.1.130 data-web
10.5.1.130 data-server
10.5.1.130 python-pro1

参考的文档
https://blog.csdn.net/yueguangMaNong/article/details/110949974

posted @ 2025-05-16 16:41  harrylearn66666  阅读(18)  评论(0)    收藏  举报