Has anybody had an experience with installation webpack dev server on Laravel 5+ (5.1 in my case)?
I'm going to use my laravel PHP backend with the ReactJS frontend and I would like to have webpack dev server on my dev env.
But I'm confused with a lot of configs in NodeJS (I'm specialized in PHP backend).
Is it generally possible to combine webpack dev server with PHP application?
I want my env to work both ways: on my apache server (for backend debugging/development) and on NodeJS server (for frontend debugging/development).
Do I need to have some middleware, resolving specific port for webpack? How in general NodeJS server will load my PHP scripts? ... or apache web server would load page than NodeJS would push notifications to frontend?
- New answer -
Since I first answered this question I've started using a different solution.
With the new solution you make requests directly to an nginx/apache web server. The web server works as a proxy and redirects requests to either webpack-dev-server or the php application. The php application exposes all it's endpoints under /api/<actual/endpoint> (see untested example configurations below, where localhost:8080 refers to webpack-dev-server).
Apache config (http://php-application refers to a separate VirtualHost, not shown here)
<VirtualHost *:80>
ServerName my-website.dev
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
ProxyPassMatch ^\/api\/.+$ http://php-application/
ProxyPassReverse / http://php-application/
</VirtualHost>
Nginx config (PHP7.1)
server {
listen 80;
server_name my-website.dev;
root /path/to/backend/public;
index index.php index.html;
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://localhost:8080;
}
location ~ ^/api/.+$ {
try_files /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
- Old answer -
I know you made it work, but I came across this post when I had this problem myself and, after solving it, wanted to share my solution.
I'm not using Laravel, but have a PHP backend on an apache server. I only had to make two changes in webpack.config.js to make webpack dev server work:
Change this
publicPath: __dirname + '<path_to_bundle>'
To this (note: http://localhost:8080 is the url to the webpack-dev-server)
publicPath: "http://localhost:8080/<path_to_bundle>/"
And add some proxy settings to forward requests to the php backend
devServer: {
proxy: [
{
path: /./,
target: "http://<php_backend_url>"
}
]
}
Notice that the path property is a regex that matches everything. This will cause all requests to be forwarded to the php backend. You might have to change the regex if you want the frontend to handle some requests.
The webpack dev server documentation also says that you have to change your script tags src attribute to http://localhost:8080/<path_to_bundle>/<bundleFilename.js>, but this is only necessary for me if I want to access the app from its old (apache) url in stead of localhost:8080 when using the --inline flag.
To make hot module replacement work with react:
Install react-hot-loader: npm install --save-dev react-hot-loader
Add the loader to your webpack.config.js along with your other loaders as react-hot
Now all you have to do is run webpack-dev-server --inline --hot and, hopefully, you are golden.
I had similar problem: On my desctop i have PHP server runing with Open Server and also Webpack Vue app. I wanted to have access to API with AJAX from my Vue app. So whis is my solution:
Create local domain (i don't like 'localhost', so i created loc-team.test) with Open Server (you can use XAMPP, or e.g.). Now i have access to http://loc-team.test/ajax.php from browser, but i don't have AJAX access to this URL from my Webpack dev server (http://loc-team.test:8081), because of Access-Control-Allow-Origin / CORS.
Add proxy to your devServer In webpack dev configuration include this props:
devServer: {
contentBase: 'dist_folder',
host: 'loc-team.test',
port: 8081,
overlay:{
warnings: true,
errors: true,
},
proxy: {
'/api': {
target: 'http://loc-team.test',
pathRewrite: {'^/api' : ''}
}
},
}
So in my Vue app i can make AJAX request to loc-team.test/api/ajax.php, and because of pathRewrite i will get answer from loc-team.test/ajax.php. Also i have no problem with sessions.
Read more about proxy at webpack.js.org
Related
Working on a link shortening website. Site works as intended in my localhost production environment, but I can't seem to get an Express GET route with query parameters working after enabling Nginx on my deployed Digital Ocean Ubuntu Linux server.
Node.js/Express GET route:
router.get("/:code", async (req, res) => {
try {
const url = await Url.findOne({ urlCode: req.params.code });
if (url) {
return res.redirect(url.longUrl);
} else {
return res.status(404).json("no url found");
}
} catch (error) {
console.error(error);
res.status(500).json("server error");
}
});
Nginx config file (etc/nginx/sites-available/default):
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
server_name myname.com www.myname.com;
location / {
proxy_pass http://localhost:3000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_cache_bypass $http_upgrade;
}
If I change the localhost port to my Express server (7777), the GET route works with the URL query parameter (ie: http://example.com/random8chars), but the React front end doesn't load.
As currently configured (port 3000/React server), a Postman GET route to "/:code" returns the desired result, but when I enter the converted link into the URL bar in Chrome it returns the default splash page. In fact, when I enter ANY extension beyond my site name in Chrome it always shows the default splash page. I know this is an issue with Nginx, but I can't seem to get it to work.
Been working on it all day to no avail. Found multiple Stack Overflow threads touching on the subject but nothing works. I tried adding a second location route to the Nginx config file, to no avail. an example of what I've tried:
location /:code {
proxy_pass http://localhost:7777/:code;
}
Please help! I am stuck and feel like I am so close to getting this working. I would greatly appreciate any insight into fixing this. Thank you.
I was just add a comment, but I don't have the rep yet. So it seems to me that Nginx is trying to serve another file, that doesn't exists. Try to add:
location / {
try_files $uri /index.html;
}
This will make nginx always try in the rigth file ignoring the path.
so i finally solved the issue! i was initially approaching this problem from the wrong perspective. see, when in my development environment, i had a proxy set up on my react server to bounce any unknown requests to my express server. the thing is, that proxy set in the package.json doesn't work in a production environment, which is why my link shortening app worked in dev but wasn't duplicated in production.
this is how i solved it:
i set up nginx to point to my express server # port 7777. i used the npm build command to export my react application to the build folder, and then i set up my express server to serve the build folder. that's it. when you access the website's default URL it loads the react build folder and then when i put in a shortened link (GET /:code) it works like a charm. thanks!
My react app has got different routes to consume different functionalities
e.x.
localhost:3000,
localhost:3000/Dashboard,
localhost:3000/Hub,
localhost:3000/Person
etc....
I wanted to configure the react app routes in the nginx in the production environment. What I have actually done so far in the nginx configuration at production env is,
server_name api.vesta-project.net;
location /vqcc {
proxy_pass http://localhost:3000/;
}
My problem here is with the current settings, the homepage works well when I say "api.vesta-project.net/vqcc". Whereas, when I click a button that navigates to /Dashboard. I get 404 error as it does not append "vqcc" to the path in the react app internally thus it becomes like api.vesta-project.net/Dashboard" when inspecting the request which is wrong per nginx conf. So I need a solution whenever the client make a request, it should append "vqcc" to the path so that it will become a valid url as per nginx routes.
e.x when client request for api.vesta-project.net/Dashboard, it should become
api.vesta-project.net/vqcc/Dashboard
Pls help me if I can handle this at nginx or package.json without being changing any routes in the react app internally
You can try rewriting the uri in catch-all location.
location / {
rewrite /(.*) /vqcc/$1;
}
location /vqcc{
proxy_pass http://localhost:3000/;
}
I've got my build folder on my server and I assume I've correctly configured nginx to be able to see it (I copied the config from Deploy Create-React-App on Nginx), but I still get a 404 page when I try to navigate to it. Is my nginx file just not configured right? I can't figure out what could be causing this problem.
I've tried following Deploy Create-React-App on Nginx exactly, changing things to match my own server name etc. when appropriate.
This is the section I've added to my config file:
server {
listen 80;
server_name xxx.net www.xxx.net;
root /var/www/xxx.net/html/build;
index index.html;
location /news {
try_files $uri /index.html;
}
}
I'm running a node.js API on a VPS, which is served by nginx on Ubuntu 13.04.
I'm using restify and serving static files like this:
server.get(
/\/static\/?.*/,
restify.serveStatic({
directory: __dirname // => /home/misha/rxviz-api
})
);
Here is the relevant bits from nginx config:
server {
listen 80;
server_name api.rxviz.com;
location / {
proxy_pass http://localhost:4010/;
}
}
(full config is here)
When running:
curl http://localhost:4010/static/.well-known/acme-challenge/test.json
on the VPS, I get the contents of test.json.
However, when navigating to:
http://api.rxviz.com/static/.well-known/acme-challenge/test.json
in a browser, I get 404.
nginx error logs show that /opt/nginx/html/static/.well-known/acme-challenge/test.json not found.
Why does nginx trying to access test.json in /opt/nginx/html rather than /home/misha/rxviz-api?
Few more points:
static directory permissions are drwxrwxr-x
I can access http://api.rxviz.com/ in the browser successfully
It's hard for me to be sure, as I can't test it, BUT..
I think you have a problem with the nginx settings:
location /static/ {
alias /home/misha/rxviz-api/static/;
}
the /static is appended to the alias including the location part, so it's searches for /home/misha/rxviz-api/static/static
This is going to return 404 since there is no static/ within static/
try to fix to:
location /static/ {
alias /home/misha/rxviz-api/;
}
....
...
and also I wanted just to mention.
It's seems like your node server (using restify) is serving the content as you wanted (127.0.0.1:4010/static/... works as you say),
BUT note that nginx is anyway not redirecting any of the /static calls into your node, so your node restify is unused.
why?
This section:
location /static/ {
alias /home/misha/rxviz-api/static/;
}
tells nginx to NOT redirect those /static calls into your node service, and instead try to find and serve the file from the local path.
so if you want to use the restify... just remove this entire location /static part, and your node will serve those files too.
I am completely stuck with a situation where I want to have several node applications on one server. I get this working fine by having the applications running on different ports. I can access the applications by putting in the ip address with port.
I would like to proxy the applications from my nginx server by using different sub-directories like so:
my.domain
location /app1 {
proxy_pass http://10.131.6.181:3001;
}
location /app2 {
proxy_pass http://10.131.6.181:3002;
}
Doing this I had to move the all the express routes to /app1 for application1. This works but now I am stuck with the static files.
I can now access the application with http://10.131.6.181:3001/app1 which is great, but via http://my.domain/app1 the static files are not loaded.
The static files can be accessed directly http://10.131.6.181:3001/css but not via the proxy http://my.domain/css
Ideally I would like to have the applications on different ports without the sub-directory in the express routes but only sub-directories in the proxy. I tried to put my head through the wall for the last 5 hours but didn't achieve anything.
Now I would happy if can at least get the static files via the nginx proxy.
An updated answer for anyone who needs:
instead of
location /app1 {
proxy_pass http://10.131.6.181:3001/app1;
}
use
location /app1/ {
proxy_pass http://10.131.6.181:3001/;
}
or if on local
location /app1/ {
proxy_pass http://localhost:3000/;
}
This is the correct way and this way you will not need to modify express. Express will receive only the part after /app1/
I finally worked it out after a google surge.
I added the directories to the nginx proxy_pass
my.domain
location /app1 {
proxy_pass http://10.131.6.181:3001/app1;
}
location /app2 {
proxy_pass http://10.131.6.181:3002/app2;
}
And I had to change the express applications to use the subdirectory
app.use('/app1', express.static(path.join(__dirname, 'public')));
app.use('/app1'', require('./routes'));
In the router I had to prefix all the redirects.
router.get('/logout', function (req, res) {
req.logout();
res.redirect('/app1/login');
});
The static files are called like so from html
<link rel="stylesheet" href="/app1/css/style.css"/>
A bit of a pain to change all the redirects and static url. I am sure there is a smarter way by setting a global variable in my node-express app. If anybody knows an easier way please post...