I recently had a server copied over from the previous host to an AWS server. The server is Ubuntu 14.04 and the project that I am trying to run is a Laravel 5 project. I have installed nginx as the web server, here is the default file in sites available and sites enabled:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root web/project/public;
index index.php index.html index.htm;
server_name server_domain_or_IP;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME$document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I have set cgi.fix_pathinfo = 0; in my php.ini file, as I was instructed to do.
There is a directory called "web" in which the project lives, and inside of the project folder lies the "public" directory.
When I try to go to the server's IP I get a 404 Not Found Error.
Any thoughts as to what may be going wrong?
Related
※ I'm using example.com as example
I'm developing an app with ELB and EC2.
The structure is as followed.
When an user access to /manage/*, ELB will transfer to wordpress hosted instance.
Here is the ELB setting.
About ELB it works, but after transfered to wordpress side, CSS and JS files' response show 404.
When I access to /magazine/* URL, loading animation of wordpress theme starts to work but get stuck because of not loaded css and js.
I'm guessing it's because of nginx configuration but can't solve it.
This is the content of configuration file. I just add /etc/nginx/conf.d/vhosts.conf and write some settings. I don't touch other files.
# vhosts.conf
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name example.com;
root /var/www/html;
location /magazine {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}
below /var/www/html, project's structure is as followed.
/wp-content
/wp-admin
/wp-include
index.php
wp-config.php
for other information,
I installed php-fpm.
SSL certification is set up with ACM and Route53
Additional Info
Both siteurl and home in DB table wp_options are set to https://example.com/magazine.
Result
made /var/www/html/magazine directory and move all files into it.
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name example.com;
location ~* /(wp-config|xmlrpc)\.php {
deny all;
}
location #magazine {
rewrite ^/magazine(.*) /magazine/index.php?$args;
}
location ^~ /magazine {
root /var/www/html;
index index.php index.html index.htm;
try_files $uri $uri/ #magazine;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
}
}
Since you are not serving wordpress from the root of your url, you must tell wordpress to load the assets from the subdirectory. How you can do this, is described in a wordpress support article. [1]
Did you check whether the WP_HOME and WP_SITEURL variables are set correctly?
Could you edit your question and include the URL your browser tries to load for the css and js files since it is not fully visible in the screenshot?
Edit:
Thanks to the additional information we know that wordpress constructs the path as expected. So, now I share your opinion that nginx is causing the issue.
I looked around and noticed two possible issues with the nginx configuration:
The root statement should probably be inside the location block. I do not know if this is really necessary, but many people do it. [2]
The location block does a prefix match and includes the prefix magazine into the $uri variable as stated in a serverfault thread. [3]
You could strip away the prefix by using a regex as follows:
location ~ ^/magazine(.*) {
root /var/www/html;
try_files $1 $1/ /index.php?$args;
}
Note: I do not know if the directory redirect $1 and default redirect to /index.php?$args make sense for serving js/css assets. You should probably remove them if they are not necessary.
References
[1] https://wordpress.org/support/article/changing-the-site-url/#changing-the-site-url
[2] https://serverfault.com/a/258540/531099
[3] https://serverfault.com/a/455853/531099
When I enter my IP in the browser, the page will appear 502 Bad Gateway. Is this my configuration file wrong?
/etc/nginx/sites-enabled/fefault:
server {
listen 80;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name 127.0.0.1;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
include uwsgi_params;
uwsgi_pass 47.101.157.128:8000;
}
location /static{
alias /var/www/sscc2019/static/;
}
/home/sscc/sscc2019/uwsgi.ini:
[uwsgi]
socket=127.0.0.1:8000
chdir=home/sscc/sscc2019
wsgi-file=sscc2019/wsgi.py
processes=4
threads=2
master=True
pidfile=uwsgi.pid
daemonize=uswgi.log
module=sscc2019.wsgi:application
vacuum=true
virtualenv=/home/ssccenv
I have a Django application stored at /var/www/w_gm .
Now I have used Nginx + Gunicorn to deploy it.
My default conf file at
root#dev:/etc/nginx/sites-enabled# ls
default w_gm
Default conf file :
server {
listen 80 default_server;
listen [::]:80 ipv6only=on default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php7.0-cgi alone:
#fastcgi_pass 127.0.0.1:9000;
# With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
w_gm conf file :
server {
listen 80;
server_name 119.00.00.100;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /var/www/w_gm/w_gm/;
}
location / {
include proxy_params;
proxy_pass http://unix:/var/www/w_gm/w_gm.sock;
}
}
Now the issue is that, when am tying in my IP address i.e, it's redirecting me to the Django app and it's working perfectly fine. But my other files which are in var/www/html doesn't get served i.e if I have <ip.addr>/work1 , it gives me error.
Now if I edit the w_gm conf file and add a suffix, let's say server_name 119.00.00.100/abc; , which obviously is wrong, my /var/www/html files start working.
I need a solution where if I type in <ip.addr>/something, then it should redirect to Django app, else serve the files which are in var/www/html.
You currently have two servers, but it sounds like you only want one server.
<ip.addr>/something is ambiguous, so you need Nginx to look for files in one root and send requests to the proxy only if they are not found.
You would need to combine your two server blocks into something like this:
root /var/www/html;
location / {
try_files $uri $uri/ #proxy;
}
location ~ \.php$ {
...
}
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /var/www/w_gm/w_gm/;
}
location #proxy {
include proxy_params;
proxy_pass http://unix:/var/www/w_gm/w_gm.sock;
}
This assumes that the /static/ URI is unambiguous, and that no URIs ending with .php are sent to the proxy. See this docuemnt for more.
I want to configure nginx for domain as well as sub domain on ubuntu as follows
mysite.in pointing to website
demo.mysite.in pointing to demo project
uat.mysite.in pointing to test project
My nginx conf looks like:
server {
listen 80 defaultserver;
listen [::]:80 defaultserver ipv6only=on;
server_name mysite.in;
location / {
root /home/amita/Website/website-v1;
try_files $uri$args $uri$args/ $uri $uri/ /index.html =404;
}
}
server {
listen 8087;
listen [::]:8087 default_server ipv6only=on;
server_name demo.mysite.in;
location / {
root /home/amita/Project/frontend/demo/project/dist;
try_files $uri$args $uri$args/ $uri $uri/ /index.html =404;
}
}
server {
listen 8088;
listen [::]:8088 default_server ipv6only=on;
server_name test.mysite.in;
location / {
root /home/amita/Project/frontend/test/project/dist;
try_files $uri$args $uri$args/ $uri $uri/ /index.html =404;
}
}
mysite.in , demo.mysite.in, test.mysite.in all shows up webiste.
I am unable to rectify mistake. Please help !!
You misspelled default_server in the first server block. The directive default_server applied to a server block means this server will respond to any request for which no specific server has been found to respond. It is commonly used in virtual hosting configurations.
You can also run nginx -t every time you edit your configuration to check if there are any new errors.
In my configuration i use the host-name($host) as directory so i don't need to make a entry for each domain in the config (only for development purpose)
one issue is that try_files failed i tried different ways but all of them without success.
try_files html\$host$uri =404;
try_files $document_root\$host$uri =404;
try_files $uri =404;
try_files $document_root\$host$fastcgi_script_name =404;
server {
listen 80;
server_name default;
charset UTF-8;
#access_log logs/host.access.log main;
# redirect server error pages to the static pages
error_page 500 502 503 504 /error/50x.html;
error_page 404 = /error/404.html;
error_page 403 = /error/403.html;
location ^~ /error/ {
internal;
root html/default;
}
location / {
root html/$host;
index index.php index.html index.htm;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
#try_files html\$host$uri =404;
#try_files $document_root\$host$uri =404;
#try_files $uri =404;
#try_files $document_root\$host$fastcgi_script_name =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root\$host$fastcgi_script_name;
include fastcgi_params;
}
The concept it self is correct, but you are applying it in the wrong place, you should place this as a root for the server, not in the try_files
Also I believe you should use $http_host not $host, refer to this answer to know the difference
I don't know why your paths are not absolute, they should start with /
I'll add changes to your current posted config
server {
#since you will match multiple domains I removed the server_name
listen 80 default_server;
charset UTF-8; # don't really need this but it won't hurt.
root /usr/share/nginx/html/$http_host; #assuming this path
index index.php index.html index.htm;
# redirect server error pages to the static pages
error_page 500 502 503 504 /error/50x.html;
error_page 404 = /error/404.html;
error_page 403 = /error/403.html;
location ^~ /error/ {
internal;
root /usr/share/nginx/html/default;
}
location / {
try_files $uri $uri/ /index.php$is_args$query_string;
}
location ~ \.php$ {
# these are the minimal required parameters.
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
}