nginx安装
一,下载解压步骤这里省略
二、安装
root用户进到nginx目录输入以下指令
./configure
make && sudo make install
三、设置开机自动启动
vi /etc/systemd/system/nginx.service
[Unit]
Description=nginx-server
After=network.target[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true[Install]
WantedBy=multi-user.target
重载系统服务
systemctl daemon-reload
开启开机启动
systemctl enable nginx
四、以下是nginx配置文件根据项目需求写
#user nobody;
worker_processes 1;#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;client_max_body_size 20m;#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;upstream myserver {server 127.0.0.1:8080;}server {listen 80;server_name 192.168.0.106; //这里是服务器ip#charset koi8-r;#access_log logs/host.access.log main;location / {alias /srv/www/;# 用于配合 browserHistory使用try_files $uri $uri/ /index.html;#禁止缓存不然更新文件,不出效果# add_header Cache-Control no-cache;# add_header Cache-Control private; }
location /abc/ { #后台接口地址(我们部署去掉了/abc项目名,如果你有请加上)注意 这个abc是路由路径proxy_pass http://myserver/abc/;proxy_redirect off;#真实IP获取proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;set $my_proxy_add_x_forwarded_for $proxy_add_x_forwarded_for;if ($proxy_add_x_forwarded_for ~* "127.0.0.1"){set $my_proxy_add_x_forwarded_for $remote_addr;}proxy_set_header X-Forwarded-For $my_proxy_add_x_forwarded_for;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# include fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {# listen 8000;# listen somename:8080;# server_name somename alias another.alias;# location / {# root html;# index index.html index.htm;# }#}# HTTPS server##server {# listen 443 ssl;# server_name localhost;# ssl_certificate cert.pem;# ssl_certificate_key cert.key;# ssl_session_cache shared:SSL:1m;# ssl_session_timeout 5m;# ssl_ciphers HIGH:!aNULL:!MD5;# ssl_prefer_server_ciphers on;# location / {# root html;# index index.html index.htm;# }#}}
注意:用systemctl启动服务nginx,网站报403错,直接命令运行nginx启动没错。两个原因,1,启动用户和用户名不一样,conf加user=root(与这个无关)。2,主要原因还是selinux的问题
403报错,权限问题,
巨坑:SELinux设置为开启状态(enabled)的原因。