Article directory
- centos7 server installation and configuration nginx
- server configuration instructions
- Final nginx configuration file
centos7 server installation and configuration nginx
1. Installation: yum -y install nginx
yum -y install nginx
Install nginx under Centos 7, use yum install nginx, it prompts that no package is available.
18 (flaskApi) [root@67 flaskDemo]# yum -y install nginx
19 已加载插件:fastestmirror, langpacks
20 Loading mirror speeds from cached hostfile
21 * base: mirrors.aliyun.com
22 * extras: mirrors.aliyun.com
23 * updates: mirrors.aliyun.com
24 没有可用软件包 nginx。
25 错误:无须任何处理
The reason is that nginx is located in the third-party yum source, not in the centos official yum source
Many software packages are not available in yum. The solution is to use the epel source, that is, install the epel-release package. EPEL (Extra Packages for Enterprise Linux) is a Fedora-based project that provides additional software packages for the "Red Hat" operating system, suitable for RHEL, CentOS and other systems. The corresponding system version and architecture software package can be found on the following website
The solution is to install epel
sudo yum install epel-release
Update (the update time is relatively long, please be patient)
yum update
yum error: protect multi-library version
When executing yum update, an error "Protecting multiple database versions" is reported
Solution: Add –setopt=protected_multilib=false parameter
yum update --setopt=protected_multilib=false
Yum installation error - Transaction check error: conflicts with
Problem: An error occurs
Transaction check error:
file /etc/libvirt/libvirt.conf from install of libvirt-libs-4.5.0-23.el7_7.6.x86_64 conflicts with file from package libvirt-client-1.1.1-29.el7.x86_64
We can check some relevant information
[root@localhost ~]# rpm -qa | grep libvirt*
[root@localhost ~]# rpm -qi libvirt-client-1.1.1-29.el7.x86_64
[root@localhost ~]# rpm -ql libvirt-client-1.1.1-29.el7.x86_64
…………省略
Solution: The method is to delete the unnecessary version (lower version)
yum -y remove libvirt-client-1.1.1-29.el7.x86_64
Try again:
yum install -y nginx
已安装:
nginx.x86_64 1:1.16.1-1.el7
作为依赖被安装:
centos-indexhtml.noarch 0:7-9.el7.centos dejavu-fonts-common.noarch 0:2.33-6.el7
dejavu-sans-fonts.noarch 0:2.33-6.el7 fontconfig.x86_64 0:2.13.0-4.3.el7
fontpackages-filesystem.noarch 0:1.44-8.el7 gd.x86_64 0:2.0.35-26.el7
gperftools-libs.x86_64 0:2.6.1-1.el7 libX11.x86_64 0:1.6.7-2.el7
libX11-common.noarch 0:1.6.7-2.el7 libXau.x86_64 0:1.0.8-2.1.el7
libXpm.x86_64 0:3.5.12-1.el7 libjpeg-turbo.x86_64 0:1.2.90-8.el7
libxcb.x86_64 0:1.13-1.el7 libxslt.x86_64 0:1.1.28-5.el7
nginx-all-modules.noarch 1:1.16.1-1.el7 nginx-filesystem.noarch 1:1.16.1-1.el7
nginx-mod-http-image-filter.x86_64 1:1.16.1-1.el7 nginx-mod-http-perl.x86_64 1:1.16.1-1.el7
nginx-mod-http-xslt-filter.x86_64 1:1.16.1-1.el7 nginx-mod-mail.x86_64 1:1.16.1-1.el7
nginx-mod-stream.x86_64 1:1.16.1-1.el7
完毕!
2. Several default directories of nginx after successful installation
view version
$ nginx -V
$ whereis nginx
ginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man3/nginx.3pm.gz /usr/share/man/man8/nginx.8.gz
Execution directory: /usr/sbin/nginx
Module directory: /usr/lib64/nginx/modules
Configuration directory: /etc/nginx/
Default site directory: /usr/share/nginx/html
Main configuration file: /etc/nginx/nginx.conf points to: /etc/nginx/conf.d/default.conf
PID directory: /var/run/nginx.pid
Error log: /var/log/nginx/error.log
Access log: /var/log/nginx/access.log
3. View nginx status (before it starts)
systemctl status nginx.service
4. Start, stop, and reload commands
systemctl start nginx.service
systemctl stop nginx.service
systemctl reload nginx.service
systemctl status nginx.service
systemctl enable nginx.service # 开机启动
Note: After configuring Nginx.conf and restarting Nginx, it will take some time to take effect.
5. View the status, process and port of nginx (after startup)
systemctl status nginx.service
The above nginx has been installed successfully
centos7 bash: netstat: command not found
Check which service occupies port 80
$ netstat -antp | grep :80
netstat -antp: 未找到命令
$ yum -y install net-tools
$ netstat -antp | grep :80
View all port usage
$ netstat -antp | grep :
View the running status of the nginx process
$ ps aux | grep nginx
Check port usage
$ lsof -i:80
-bash: lsof: 未找到命令
install lsof
$ yum -y install lsof
$ lsof -i:80
kill process command
$ killall -9 nginx // 相关nginx进行全部杀掉
$ kill -9 pid1 and kill -9 pid1 // 把PID两个进程杀掉:
server configuration instructions
$ whereis nginx
# /etc/nginx
$ cd /etc/nginx # 配置文件所在目录
$ vi nginx.conf
Add 2 server configurations
:wq save and exit editing
Restart nginx or reload
service nginx restart
service nginx reload
403 Forbidden appears in nginx
Visit bbs.yueyouji.cn:8080, 403 occurs, the solution is as follows
1. Check whether the file directory is in the default directory (/usr/share/nginx/html). Generally, this problem occurs because the file is not in the default folder
2. If you make a usr folder, check whether the folder has 777 permissions, if not, give permissions, such as: chmod -R 755 /var/www
3. If the file is in the root directory, execute: vim /etc/nginx/nginx.conf, modify the first line of the file:
user root; Give nginx user root permission
Enable reverse proxy proxy_pass
Forward port 80 of the corresponding domain name to 3000 to access the node.js application through nginx, and it can also be forwarded to other ports such as java in the background.
server
{
listen 80;
#listen [::]:80;
server_name domain.com ;
location / {
proxy_http_version 1.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://127.0.0.1:3000;
}
}
streamlined
vi /etc/nginx/nginx.conf
root /usr/share/nginx/html
server{
listen 80;
server_name _;
location / {
proxy_http_version 1.1;
proxy_pass http://127.0.0.1:3000;
}
}
server {
listen 80;
server_name _;
root /home/web/vue/dist;
index index.html index.htm;
location / {
try_files $uri $uri/ @router;
index index.html index.htm;
}
location @router {
rewrite ^.*$ /index.html last;
}
location ~/api/ {
proxy_http_version 1.1;
proxy_pass http://127.0.0.1:3000;
}
}
Configure vue project: try_files and rewrite
location / {
try_files $uri $uri/ @router;
index index.html index.htm;
}
location @router {
rewrite ^.*$ /index.html last;
}
nginx permission problem failed(13:Permission denied)
open nginx.conf
user nginx;
Change touser root;
Final nginx configuration file
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
log_format vue-app '$remote_addr,$time_iso8601,$request,'
'$http_referer,$http_x_forwarded_for';
access_log /var/log/nginx/access.log vue-app;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
# include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
server_name _;
root /usr/share/nginx/html/vue-app;
index index.html index.htm;
location / {
try_files $uri $uri/ @router;
index index.html index.htm;
}
location @router {
rewrite ^.*$ /index.html last;
}
location ~ /api/ {
proxy_http_version 1.1;
proxy_pass http://127.0.0.1:3000;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
After Alibaba Cloud deploys multiple applications
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
# include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
log_format vue-app '$remote_addr,$time_iso8601,$request,'
'$http_referer,$http_x_forwarded_for';
access_log /var/log/nginx/access.log vue-app;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
# root /root/web/vue-app;
# Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
location / {
root /root/web/vue-app;
try_files $uri $uri/ @router;
index index.html index.htm;
}
location /backend {
alias /root/web/vue-app-backend/dist;
try_files $uri $uri/ @router;
index index.html index.htm;
}
location @router {
rewrite ^.*$ /index.html last;
}
location ~ /api/ {
proxy_pass http://127.0.0.1:3000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Connection "";
}
location ~ /api2/ {
proxy_pass http://127.0.0.1:3001;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Connection "";
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}