nuxt-community / redirect-module

No more cumbersome redirects for Nuxt 2!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Module not working in production after deploying

vserdobintsev opened this issue · comments

When I run the project locally in dev or prod mode, everything works. But after I deploy it on a remote server, the module does not work.

The same problem caught up with me when using the @nuxtjs/sitemap module. But you can still survive this.

Would you provide a reproduction? How is your site deployed? Might it be in your buildModules or devDependencies?

The web server is configured in this way: nginx proxies requests to localhost:3000, where the project is deployed using pm2.
I use skeleton Nuxt and Laravel: https://github.com/cretueusebiu/laravel-nuxt

nuxt.config.js

I will shorten the code of my nuxt.config.js to make it easier to learn.

`
import path from 'path'
import fs from 'fs'
import CopyWebpackPlugin from 'copy-webpack-plugin'

require('dotenv').config()
const { join } = require('path')

module.exports = {
ssr: true,

srcDir: __dirname,

server: {
https: {
key: fs.readFileSync(path.resolve(__dirname, 'domain.key')),
cert: fs.readFileSync(path.resolve(__dirname, 'domain.crt'))
}
},
env: {
// ...
},
head: {
htmlAttrs: {
// ...
},
meta: [
// ...
],
script: [
// ...
],
link: [
// ---- favicons ----
{ rel: 'shortcut icon', sizes: '32x32', type: 'image/x-icon', href: '/_nuxt/meta/icons/favicon.ico' },
]
},
plugins: [
'~components/global',
'~plugins/i18n',
'~plugins/fontawesome',
'~plugins/axios',
'~plugins/nuxt-client-init',
'~plugins/vue-burger',
'@plugins/v-mask.js',
'~plugins/pages-core',
{ src: '~plugins/vue-ymaps', mode: 'client' }
],
sitemap: {
gzip: true,
cacheTime: 1000 * 60 * 60 * 24,
defaults: {
lastmod: process.env.SITEMAP_LASTMOD,
changefreq: 'weekly'
},
async routes () {
const routes = (await import('./utils/routes/routes')).default
const result = []
for (let index = 0; index < routes.length - 1; index++) {
const route = routes[index]
if ('children' in route) {
for (let childIndex = 0; childIndex < route.children.length; childIndex++) {
const child = route.children[childIndex]
result.push(child.name.replace('.', '/').replace('index', ''))
}
continue
}
result.push(route.path)
}
return result
}
},
redirect: [
{
from: '^/service/(.*)$',
to: '/services/$1',
statusCode: 301
},
...require('./config/redirects.json')
],
modules: [
'@nuxtjs/router',
'vue-sweetalert2/nuxt',
'@nuxtjs/style-resources',
'@nuxtjs/axios',
['@nuxtjs/html-minifier', { log: 'once', logHtml: true }],
['@naumstory/nuxtjs-yandex-metrika', {
id: process.env.YANDEX_METRIKA_ID,
webvisor: true,
clickmap: true,
trackLinks: true,
accurateTrackBounce: true
}],
'@nuxtjs/sitemap',
'@nuxtjs/robots',
'@nuxtjs/redirect-module'
],
build: {
extractCSS: true,
plugins: [
new CopyWebpackPlugin([
{ from: './client/static/meta', to: './meta' }
])
]
},
buildModules: [
['@nuxtjs/vuetify', {
theme: { disable: true },
treeShake: true,
defaultAssets: false,
icons: {
iconfont: 'md'
}
}]
]
}
`

dependencies and devDependencies

{
"dependencies": {
"@fortawesome/fontawesome-free": "^5.15.2",
"@fortawesome/fontawesome-svg-core": "^1.2.34",
"@fortawesome/free-brands-svg-icons": "^5.15.2",
"@fortawesome/free-regular-svg-icons": "^5.15.2",
"@fortawesome/free-solid-svg-icons": "^5.15.2",
"@fortawesome/vue-fontawesome": "^2.0.2",
"@naumstory/nuxtjs-yandex-metrika": "^1.0.5",
"@nuxtjs/axios": "^5.13.1",
"@nuxtjs/html-minifier": "^0.1.2",
"@nuxtjs/redirect-module": "^0.3.1",
"@nuxtjs/robots": "^2.5.0",
"@nuxtjs/router": "^1.5.0",
"@nuxtjs/sitemap": "^2.4.0",
"axios": "^0.21.1",
"copy-webpack-plugin": "github:webpack-contrib/copy-webpack-plugin#v5",
"cssnano": "^4.1.10",
"dotenv": "^8.2.0",
"fs": "^0.0.1-security",
"js-cookie": "^2.2.1",
"json-loader": "^0.5.7",
"lunr": "^2.3.9",
"nuxt": "^2.15.7",
"pexels": "^1.0.1",
"sweetalert2": "^10.14.0",
"typescript": "^4.1.3",
"v-mask": "^2.2.4",
"vform": "^1.0.1",
"vue": "^2.6.14",
"vue-burger": "^1.1.0",
"vue-i18n": "^8.22.4",
"vue-server-renderer": "^2.6.14",
"vue-sweetalert2": "^4.2.0",
"vue-yandex-maps": "^0.10.12"
},
"devDependencies": {
"@babel/eslint-parser": "^7.12.13",
"@nuxtjs/eslint-config": "^5.0.0",
"@nuxtjs/style-resources": "^1.0.0",
"@nuxtjs/vuetify": "^1.11.3",
"eslint": "^7.19.0",
"fs-extra": "^9.1.0",
"node-sass": "^5.0.0",
"postcss-combine-media-query": "^1.0.1",
"sass-loader": "^10.1.1"
}
}

nginx conf file

upstream pm2_my_domain {
server 127.0.0.1:3000;

}

server {
listen 80;
server_name my-domain.com;
rewrite ^ https://$host$request_uri permanent;
}

server {
listen 80;
server_name www.my-domain.com;
rewrite ^ https://$host$request_uri permanent;
}

server {
listen 443 ssl http2;
server_name my-domain.com;
root /var/www/project/;
index index.php index.html index.htm;
access_log /var/www/logs/ssl-access.log main;
error_log /var/www/logs/ssl-error.log;

keepalive_timeout		60;
ssl_certificate		/etc/letsencrypt/live/my-domain.com/fullchain.pem;
ssl_certificate_key		/etc/letsencrypt/live/my-domain.com/privkey.pem;
ssl_protocols 		TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers '-------------SOME DATA----------------';
ssl_dhparam 		/etc/ssl/certs/dhparam.pem;
add_header			Strict-Transport-Security 'max-age=604800';

gzip on;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_min_length 1100;
gzip_buffers     4 8k;
gzip_proxied any;
gzip_types
# text/html is always compressed by HttpGzipModule
text/css
text/javascript
text/xml
text/plain
text/x-component
application/javascript
application/json
application/xml
application/rss+xml
font/truetype
font/opentype
application/vnd.ms-fontobject
image/svg+xml;

gzip_static on;

gzip_proxied        expired no-cache no-store private auth;
gzip_disable        "MSIE [1-6]\.";
gzip_vary           on;

location ~ /\.ht {
deny all;
}
location ^~ /content {
alias /var/www/project/public/content/;
expires 30d;
}
location = /robots.txt {
alias /var/www/project/public/robots.txt;
}
location = /sitemap.xml {
alias /var/www/project/public/sitemap.xml;
}
location /phpmyadmin {
  alias /usr/share/phpMyAdmin/;
  location ~ \.php$ {
    fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    include fastcgi_params;
    fastcgi_ignore_client_abort off;    
  }
  location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    access_log    off;
    log_not_found    off;
    expires 1M;
  }
}
location / {
# Force index.php routing (if not found)
# try_files $uri $uri/ /index.php?$query_string;

# Force index.php routing (all requests)
# rewrite ^/(.*)$ /index.php?/$1 last;

# limit_conn   addr 16;
# limit_req    zone=flood        burst=32 nodelay;

# add_header   X-Frame-Options   'SAMEORIGIN' always;
# add_header   Referrer-Policy   'no-referrer-when-downgrade' always;

# CSP syntax: <host-source> <scheme-source>(http: https: data: mediastream: blob: filesystem:) 'self' 'unsafe-inline' 'unsafe-eval' 'none'
# Content-Security-Policy-Report-Only (report-uri https://site.com/csp/)
# add_header   Content-Security-Policy  "default-src 'self'; connect-src 'self'; font-src 'self'; frame-src 'self'; img-src 'self'; manifest-src 'self'; media-src 'self'; object-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; base-uri 'none'; form-action 'self'; frame-ancestors 'self'; upgrade-insecure-requests" always;


proxy_set_header          Host $host;
proxy_set_header          X-Forwarded-For $http_x_forwarded_for;
proxy_set_header          X-Real-IP $remote_addr;
proxy_set_header          X-Forwarded-Proto $scheme;
proxy_http_version        1.1;
proxy_set_header          Upgrade $http_upgrade;
proxy_set_header          Connection 'upgrade';
proxy_cache_bypass        $http_upgrade;
proxy_pass                http://pm2_my_domain;
}

location /_nuxt/ {
    alias /var/www/project/.nuxt/dist/client/;
}

location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|txt|srt|swf|woff|woff2|svg)$ {
    rewrite ^/_nuxt(/.*) $1 break;
    root /var/www/project/.nuxt/dist/client/;
    expires 30d;
}
location ~* ^.+.(js|css|png|jpg|jpeg|gif|ico|woff)$ {
access_log off;
expires max;
}

location /api/ {
fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/project/public/index.php;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
include fastcgi_params;
fastcgi_param HTTPS on;
}

location = /favicon.ico {
log_not_found off;
access_log off;
}

}

server {
listen 443 ssl http2;
server_name www.my-domain.com;
rewrite ^ https://my-domain.com$request_uri? permanent;
}

@danielroe, I found your comments on this topic here nuxt/nuxt#9158 (comment) and here nuxt/nuxt#8230 (comment).
What do you recommend? Is there any sample code that can be written to start middleware in production mode deployed on the server?

Hey! did you ever fix this?

Hey! did you ever fix this?

As far as I remember, I did not find a solution