上述请求 uri 为/product/detail,根据 try_files 配置,匹配如下: a. $uri进行匹配,资源文件不存在; b. 向下$uri/匹配,发现 detail 文件夹存在,则返回 301 地址https://www.kch8.top/product/detail/?productId=100000(detail 文件下存在默认 index.html 文件,并没有匹配 index.html 内容,这和 location 下 uri 匹配有区别) c. b 步骤不匹配,则找根目录下 index.html,最后这个 uri 一定要存在,否则会报 404 错误;(最后这个匹配是内部重定向,而不是下发 301 或 302 这种)
Enables or disables the use of the primary server name, specified by the server_name directive, in absolute redirects issued by nginx. When the use of the primary server name is disabled, the name from the “Host” request header field is used. If this field is not present, the IP address of the server is used.
重定向 url 中的端口,可以通过port_in_redirect控制,默认开启,官方解释:
1
Enables or disables specifying the port in absolute redirects issued by nginx.
关闭绝对重定向,可以通过absolute_redirect控制,默认开启,官方解释:
1
If disabled, redirects issued by nginx will be relative.
Example of deployment process which I use in my Nuxt.js projects. I usually have 3 components running per project: admin-panel SPA, nuxt.js renderer and JSON API.
This manual is relevant for VPS such as DigitalOcean.com or Vultr.com. It’s easier to use things like Now for deployment but for most cases VPS gives more flexebillity needed for projects bigger than a landing page.
UPD: This manual now compatible with nuxt@2.3. For older versions deployment, see revision history.
Let’s assume that you have entered fresh installation of Ubuntu instance via SSH. Let’s rock:
1.Initial setup.
Depending on size of project you have two options: make non-root user or go on with root. I’d suggest you to go with root, which is easier (except nginx part). If you have chosen non-root, check out this tutorial.
It’s actually up to you, depending on type of your project, but I usually use this structure:
user dir (ex., /root)
config.yml (pm2 config)
app (project name)
client (nuxt.js files)
server (API server files)
public (static content)
Fill server dir with your API code, in my case it’s nodejs + koa2 + mongoose stack. Place at least favicon and robots.txt to public dir.
4.Upload nuxt.js bundle.
Run npm run build on your local machine. I don’t recommend to build nuxt.js on production server, because it eats lots of memory and causes up to minute of downtime. Take package.json, nuxt.config.js and .nuxt dir and copy them via SFTP (or pull from git) to client dir.
Note that there is a special Nuxt package for running production renderer without useless overhead - nuxt-start. Intall it as dependency with the same version as your Nuxt package. Move to your client directory on the server and install production dependencies (in most cases you only need nuxt-start package): npm i -—production.
5.Set up PM2
PM2 is a process manager for node.js. Install PM2 and create config file in your user root dir: config.yml. See config example below. Don’t forget to run pm2 save && pm2 startup, so your processes will recover on server restart.
6.Set up Nginx
Install nginx:
sudo apt-get update
sudo apt-get install nginx
(If you use root user, you have to set right permissions for project dirs to make it work with static files. Or just change user: www-data to user: root in /etc/nginx/nginx.conf.) Then edit config file /etc/nginx/sites-available/default, see config example below. Don’t forget to sudo nginx -s reload after every nginx config modification.
If you have already connected domain to your project, it’s super easy to set up https (and http2). See this tutorial for installing certbot.
7.Fire it up!
Move to dir that contains your pm2 config file and run pm2 start config.yml –-env production. Yay, everything should work now, but it doesn’t. Run pm2 logs to see errors. This manual is complicated for a newbie and imperfect itself, so you will probably have to try several times. But it’s worth it.
(Don't forget to remove comments) --- apps: - name: client # process name. You will use it to make commands such as pm2 restart client script: node_modules/nuxt-start/bin/nuxt-start.js # path to nuxt-start renderer from root nuxt dir. Don't forget to install nuxt-start dependency cwd: /root/app/client # absolute path to nuxt dir max_memory_restart: "250M" # in case nuxt renderer eats all memory, it will be restarted args: "start" # command to skip build and start renderer env_production: PORT: 3000 # local port. Same port should be set in nginx config NODE_ENV: production # in case of enviroment variables usage
# API server, if you have one - name: server script: app.js cwd: /root/app/server env_production: PORT: 3001 NODE_ENV: production
# entry point for API server, if you have one location /api { proxy_pass http://localhost:3001; client_max_body_size 3m; }
# entry point for SPA admin page, if you have one location /admin { try_files /admin/index.html =404; }
# serve nuxt bundle with max cache life. Only compatible with nuxt 2.*. For 1.*, remove last 'client' from alias location ~^\/_nuxt(.*)$ { alias $root_path/client/.nuxt/dist/client/$1; gzip on; gzip_comp_level 6; gzip_vary on; gzip_types text/css application/json application/javascript text/javascript application/x-font-ttf font/opentype; expires max; }