Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nginx 代理的问题 #12

Closed
zhans1993 opened this issue Feb 28, 2018 · 15 comments
Closed

nginx 代理的问题 #12

zhans1993 opened this issue Feb 28, 2018 · 15 comments

Comments

@zhans1993
Copy link

我现在把工程发布之后,需要使用nginx 做代理,但是怎么配置都无法成功,这是我的location 配置,需要怎么更改呢? 谢谢
upstream dist {
server localhost:8888 weight=3;
}
server {
listen 8082;
server_name localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

   location /dist {
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Nginx-Proxy true;	
    proxy_set_header Connection "";
        proxy_pass http://dist;
        root   html;
        index  index.html index.htm;
    }
@javaLuo
Copy link
Owner

javaLuo commented Feb 28, 2018

访问不到还是什么
你确定index.html文件是放在/dist下的吗
把文件放到服务器上,路由访问到 index.html就行

@zhans1993
Copy link
Author

index.html 和 dist 是同级,不在dist 下面

@javaLuo
Copy link
Owner

javaLuo commented Feb 28, 2018

能访问到index.html吗

@zhans1993
Copy link
Author

zhans1993 commented Feb 28, 2018

http://localhost:8082/dist/index.html 是可以访问到index.html 的

没有代理的工程 http://localhost:8888/home 可以访问到页面
代理的nginx http://localhost:8082/dist/home 页面错误 访问的资源是http://localhost:8082/dist/dist/app.d66600.js

@javaLuo
Copy link
Owner

javaLuo commented Feb 28, 2018

额,那是什么问题,无法加载JS和CSS吗?
把webpack.production.config.js中的输出配置改成这样:
`
output: {

    path: path.resolve(__dirname, 'build/dist'), 

    publicPath: 'dist/',

    filename: '[name].[hash:6].js',

    chunkFilename: '[name].[hash:6].chunk.js',

},

`

@zhans1993
Copy link
Author

zhans1993 commented Feb 28, 2018

是的,无法加载js 和css, 现在的output 就是这样配置的
就是js css 资源 多带一层 dist
webpack 配置就是用的你写的那个
主要是想nginx 代理之后 加一层 dist 字段分隔,不直接在端口下走路由
是不是工程需要改呢?

@javaLuo
Copy link
Owner

javaLuo commented Feb 28, 2018

等我配个nginx试一下

@javaLuo
Copy link
Owner

javaLuo commented Feb 28, 2018

没有问题额,我是这么配的
`
server {

listen 80;

server_name gd.isluo.com;

location / {

proxy_pass http://127.0.0.1:8888;

proxy_redirect off;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

error_page 404 500 502 503 504 /50x.html

location = /50x.html {

root html;

}

}
`

我在服务器上试的,用了Tomcat,Nginx代理到Tomcat.
我对Nginx也不是很了解。如果不行,你问下身边的后台程序员,后台程序员一般都知道这个。

@zhans1993
Copy link
Author

我知道这样配,问题是这样配的话一个程序就占用了一个端口,,端口有限 主要是希望 80/项目a
80/项目b 多个项目功用一个端口

@javaLuo
Copy link
Owner

javaLuo commented Feb 28, 2018

现在你这个路径访问不到 http://localhost:8082/dist/dist/app.d66600.js
是不是要配个静态文件访问路径什么的

@zhans1993
Copy link
Author

那么多资源总不能一个一个写吧,,而且怎么保证不和别的项目的资源冲突呢,就是这个愁啊

@javaLuo
Copy link
Owner

javaLuo commented Feb 28, 2018

就在你这个server{ } 里面再加一个:
location ~*/dist/{
root /dist/
}
就写一个,能访问到http://localhost:8082/dist/dist就行了
我不知道这样写对不对,大致是这个意思

@zhans1993
Copy link
Author

zhans1993 commented Feb 28, 2018

还是不行 ,我试试其他办法吧,你们项目不用nginx代理的么,有类似思路么。。谢谢
如果不代理的话,本地工程所有路由也加一层呢

@javaLuo
Copy link
Owner

javaLuo commented Feb 28, 2018

用的,但都是后台在处理。
我等会儿去问下后台是怎么弄的

@zhans1993
Copy link
Author

额,如果项目部署在服务器二级目录呢?

@javaLuo javaLuo closed this as completed Feb 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants