Nginx配置

Nginx配置静态页面

server
    {
        listen 80 default_server reuseport;
        #listen [::]:80 default_server ipv6only=on;
        server_name _;
        index index.html index.htm index.php;
        root  /home/wwwroot/typecho_blog;

        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
        include enable-php-pathinfo.conf;
        location /nginx_status
        {
            stub_status on;
            access_log   off;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }
        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }
        location ~ /.well-known {
            allow all;
        }
        location ~ /\.
        {
            deny all;
        }
        access_log  /usr/local/nginx/logs/access.log;
    }

Nginx图片服务器地址映射

用Nginx搭建图片服务时,映射地址总是报404。

图片实际存放地址是 /home/wwwroot/images/

浏览器访问地址 localhost/images/logo.jpg,返回404,配置如下:

server{
        listen: 80;
        server_name: localhost;
        location /images{  # 对外暴露的路径
            root /home/wwwroot/images/;
            autoindex on;
        }
    }

原因是 root配置后实际的访问路径是 root后面的路径 + location后面的路径 + 静态文件,所以最终访问的路径是地址是/home/wwwroot/images/images/logo.ipg,所以找不到文件。

正确配置如下,注意端口冲突

# 如果使用root配置,正确配置如下:
server{
    listen 81;
    server_name localhost;
    location /images{
        root /home/wwwroot/;
        autoindex on;
    }
}

# 也可以使用alias配置,使用全路径
server{
    listen 82;
    server_name localhost;
    location /images{
        alias /home/wwwroot/images/;
        autoindex on;
    }
}

现在访问 localhost:81/images/logo.jpg 即可访问。

Nginx端口映射配置

server {
  listen    80;
  server_name rbac.dev-lu.com;

  # 80转发到8000端口
  location / {
      proxy_pass http://127.0.0.1:8000;
  }
}

nginx反向代理-多端口映射

 server {
    listen    80;
    server_name www.baidu.test.com;#你要填写的域名,多个用逗号隔开
    location / {
      proxy_pass http://localhost:8083; 
      proxy_set_header Host $host; 
      proxy_set_header X-Real-IP $remote_addr; 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
      root  /app/esop_web/esopschool;
      index index.html;
      try_files $uri $uri/ /index.html;
    }
    location /rest{
      proxy_pass http://localhost:9803; 
      proxy_set_header  Host  $host; 
      proxy_set_header  X-Real-IP  $remote_addr; 
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for; 
    }
  }

代码解释
1.1 http:www.baidu.test.com默认是80,访问“/”利用反向代理,然后访问本地8083;
1.2 8083代表本地的前端工程访问地址,前端需要访问后台数据,”/”,继续代理到后台地址9803;
1.3 这样就做到了只要开通80端口就可以完成多个端口访问。
1.4 root配置可以是绝对路径,也可是相对路径。

参考:

https://blog.csdn.net/tt_fan/article/details/85322930
https://www.jb51.net/article/141886.htm

本文链接: https://jianz.xyz/index.php/archives/73/

1 + 8 =
快来做第一个评论的人吧~