使用proxy_pass的Nginx反向代理可用于根位置,但不能用于子目录

时间:2018-08-11 02:03:44

标签: docker nginx reverse-proxy nginx-location

几天来我一直在努力,我一直在学习Nginx的工作方式,但是我怕错过了一些非常愚蠢或简单的事情。

我有一个运行在abc.domainname.com:444的docker容器中的webapp。如果我通过该URL访问它,则它的功能非常完善。但是,我不希望URL中需要端口号,因此我一直在尝试将Nginx设置为反向端口,以便在有人访问abc.domainname.com时获得所有内容。 abc.domainname.com:444中的。经过几天的工作,我成功地从URL中获取了端口号以进行直接访问,但是docker应用正在从abc.domainname.com:444/app/dfjsdfksdfjsdkfdsj.jsabc.domainname.com:444/images/images1/fjdskds.jpg等位置加载一些资源/app//images/中的其他子目录。当我访问位于abc.domainname.com的站点时,这些资产未加载。根据我的开发者控制台,每个资源都有一个404。每个资源的URL都是正确的-如果指定了端口号,则应该是正确的。

基本上,abc.domainname.com:444/app/dfjsdfksdfjsdkfdsj.js会加载资源,但是使用下面的Nginx配置,我发现似乎只有根正在通过代理传递,因为同一资源不能称为abc.domainname.com/app/dfjsdfksdfjsdkfdsj.js

我什至试图为这些项目建立封锁,但无济于事。

我感觉到我所缺少的位置限制存在问题。有更多经验的人可以帮助我找出我的错误,以便(希望)开始理解Nginx定位块以及Nginx如何用作反向代理。

server_name             abc.domainname.com;
server_tokens           off;
error_log               /home/runcloud/logs/nginx/Webappy_error.log;
access_log              /home/runcloud/logs/nginx/Webapp_access.log main buffer=16k;
access_log              /var/log/nginx-rc/Webapp_traffic.log traffic buffer=16k;

client_max_body_size    256m;

# Header option for security purpose
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";

# HSTS
add_header Strict-Transport-Security "max-age=15768000" always;

root /home/runcloud/webapps/Webapp;
index index.php index.html index.htm;

location ~ /.well-known/acme-challenge {
    allow all;
    log_not_found off;
    root  /opt/RunCloud/letsencrypt;
}

location / {

    # Proxy config to remove the port number from the URL
    proxy_pass https://abc.domainname.com:444;
    proxy_redirect off;
    proxy_set_header Host $http_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;
    proxy_set_header X-NginX-Proxy true;

    try_files $uri $uri/ /index.php$is_args$args;
}

location ~ /\. {
    deny all;
    access_log off;
    log_not_found off;
}

location = /favicon.ico {
    log_not_found off;
}

location ~ .(ico|css|gif|jpe?g|png|gz|zip|flv|rar|wmv|avi|css|js|swf|png|htc|mpeg|mpg|txt|otf|ttf|eot|woff|svg)$ {
    expires     1M;
    # Header option for security purpose
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    # HSTS
    add_header Strict-Transport-Security "max-age=15768000" always;

    add_header  Cache-Control "public";
    # Proxy config to remove the port number from the URL
    proxy_pass https://abc.domainname.com:444$request_uri;
    proxy_redirect off;
    proxy_set_header Host $http_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;
    proxy_set_header X-NginX-Proxy true;    

    try_files $uri $uri/ /index.php$is_args$args;
}

location ~ .(html)$ {
    expires     24h;
    # Header option for security purpose
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    # HSTS
    add_header Strict-Transport-Security "max-age=15768000" always;

    add_header  Cache-Control "public";
    try_files $uri $uri/ /index.php$is_args$args;
}

location /app/ {
    # Proxy config to remove the port number from the URL
    proxy_pass https://abc.domainname.com:444/app/;
    proxy_redirect off;
    proxy_set_header X-Forwarded-Host $http_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;
    proxy_set_header X-NginX-Proxy true;

    try_files $uri $uri/ /index.php$is_args$args;
}
location /images/ {
    # Proxy config to remove the port number from the URL
    proxy_pass https://abc.domainname.com:444/images/;
    proxy_redirect off;
    proxy_set_header X-Forwarded-Host $http_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;
    proxy_set_header X-NginX-Proxy true;

    try_files $uri $uri/ /index.php$is_args$args;
}

location ~ \.php$ {
    try_files $uri               =404;
    fastcgi_split_path_info      ^(.+\.php)(/.+)$;
    fastcgi_index                index.php;
    fastcgi_param                SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass                 unix:/var/run/Webapp.sock;
    fastcgi_send_timeout         60;
    fastcgi_read_timeout         60;
    fastcgi_buffer_size          128k;
    fastcgi_buffers              4 256k;
    fastcgi_busy_buffers_size    256k;
    fastcgi_temp_file_write_size 256k;
    fastcgi_connect_timeout      30s;
    include fastcgi_params;
}

0 个答案:

没有答案